Can I create html helpers in a Webforms project, for example in asp.net mvc?

Can I create html helpers in a Webforms project, for example in asp.net mvc? Thank.

+3
source share
3 answers

You only need a static method:

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

Here is one that works for me so far in my limited use

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;
    }
+3
source

This is not as simple as adding a static method if you want to return WebControls. You will need to connect to the page rendering.

+1
source

Source: https://habr.com/ru/post/1751996/


All Articles