Can i create html helpers in Webforms project like in asp.net mvc? Thanks.
3 Answers
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;
}
2 Comments
enorl76
This answer goes a long way to bridging a gap of enabling the MVC Html statements like Html.DisplayFor and so forth.
PreguntonCojoneroCabrón
System.Web.Mvc.UrlHelper in WebForms ? full sample webforms aspx page using it ?
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
Sean Haddy
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).