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

后端 未结 2 534
孤城傲影
孤城傲影 2020-12-12 05:58

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 thi

相关标签:
2条回答
  • 2020-12-12 06:07
    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. ;)

    0 讨论(0)
  • 2020-12-12 06:09

    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;
        }
    
    0 讨论(0)
提交回复
热议问题