1

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?

1
  • Yes, I want to do this with @Html.Partial(), toward the goal of reusing header HTML markup. Commented May 18, 2015 at 22:07

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Where are the poachers when we need them?
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;
    }

3 Comments

Using this helper to render an input like this: <%= this.GetHtmlHelper().TextBox("textBox") %>, gives a NRE: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes) +43 System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name) +43 Any ideas what's missing?
in debug, what exactly is null?
Depends what ext method you use: for .TextBox(..) that'll be HttpContext (this is where it tries to get value from), for .BeginForm it will be .Writer property (so it can render opening/closing HTML tags). Final class might looks smth like: gist.github.com/esteewhy/6450385

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.