solrevdev
solrevdev

Reputation: 9033

Using ASP.NET MVC Html Helpers in a standard web forms project

So for example in the code behind of a web form aspx page I would like to be able to do things like

string textBoxHtml = Html.TextBox("MyTextBox");

Is this possible?

Is the source code available to fork for webforms?

Upvotes: 1

Views: 3195

Answers (2)

Maslow
Maslow

Reputation: 18746

Here's something that is working for me so far.

public static class PageCommon
{
    public static System.Web.Mvc.UrlHelper GetUrlHelper(this System.Web.UI.Control c)
    {
        var helper = new System.Web.Mvc.UrlHelper(c.Page.Request.RequestContext);
        return helper;
    }
    class ViewDataBag : IViewDataContainer
    {
        ViewDataDictionary vdd = new ViewDataDictionary();
        public ViewDataDictionary ViewData
        {
            get
            {
                return vdd;
            }
            set
            {
                vdd = value;
            }
        }
    }
    public static System.Web.Mvc.HtmlHelper GetHtmlHelper(this System.Web.UI.Control c)
    {
        IViewDataContainer x;

        var v = new System.Web.Mvc.ViewContext();
        var helper = new System.Web.Mvc.HtmlHelper(v, new ViewDataBag());
        return helper;
    }

Upvotes: 2

John Farrell
John Farrell

Reputation: 24754

  1. Possible? Yes.

  2. The entire MVC source is available at: http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en

Good luck!

You'll quickly find that pulling bits of code out of MVC is like only wanting a banana and getting the gorilla holding it. ;)

Upvotes: 6

Related Questions