4

Can i create html helpers in Webforms project like in asp.net mvc? Thanks.

1
  • Can you give an example of what you want and why you want it? Commented Jun 27, 2010 at 19:56

3 Answers 3

4

Here's one that is working for me so far in my limited usage

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)
    {
        var v = new System.Web.Mvc.ViewContext();
        var helper = new System.Web.Mvc.HtmlHelper(v, new ViewDataBag());
        return helper;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This answer goes a long way to bridging a gap of enabling the MVC Html statements like Html.DisplayFor and so forth.
System.Web.Mvc.UrlHelper in WebForms ? full sample webforms aspx page using it ?
2

You just need a static method:

public static string Label(string target, string text)
{
    return String.Format("<label for= '{0}'>{1}</label>",target,text);
}

Comments

1

It won't be as simple as adding a static method if you want to return WebControls. You would have to hook up to the page render.

1 Comment

Not only that, you would have to create your own intelesense to allow for simple usage of the html helpers (for it to be easy, which is the whole point of html helpers).

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.