Vlad Omelyanchuk
Vlad Omelyanchuk

Reputation: 3091

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

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

Upvotes: 4

Views: 2418

Answers (3)

Maslow
Maslow

Reputation: 18746

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

Upvotes: 4

Tomislav Markovski
Tomislav Markovski

Reputation: 12346

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.

Upvotes: 1

Paul Creasey
Paul Creasey

Reputation: 28834

You just need a static method:

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

Upvotes: 2

Related Questions