Skip to content Skip to sidebar Skip to footer

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

Solution 1:

  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. ;)


Solution 2:

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;
    }

Post a Comment for "Using ASP.NET MVC Html Helpers In A Standard Web Forms Project"