I need to convert some user control build for webform to html helpers.
So for example I need to convert the custom controls below to html helper :
public class DisGrid : Panel
{
protected override void Render(HtmlTextWriter writer)
{
writer.WriteLine("<div class=\"disgrid\"><div>");
base.Render(writer);
writer.WriteLine("</div></div>");
}
}
How Can I do this?
I did it this way but I got an error saying that DisclaimerWidget does not implement inherited abstract member 'project1.Helpers.HtmlWidget.BeginWidget()'
public class DisGridWidget : HtmlWidget
{
public DisGridWidget(ViewContext viewContext) : base(viewContext)
{
this.BeginWidget();
}
protected void BeginWidget()
{
var sb = new StringBuilder();
sb.AppendFormat("<div class='disgrid'><div>");
this._textWriter.WriteLine(sb.ToString());
}
protected override void EndWidget()
{
this._textWriter.WriteLine("</div></div>");
}
}
or there s another way to do it ?
and how to use it in a view?
To answer your first question, change protected void BeginWidget()
to protected override void BeginWidget()
As for your second question, I'm unfamiliar with the HtmlWidget
base class. The way I've seen widgets done in the past is by calling @Html.Partial()
or using Html.Action()
. I believe there are some open source projects out there that add an HtmlHelper
extension for widgets as well.
http://mvc4beginner.com/Tutorial/MVC-Partial-Views.html is a great resource to introduce Html.Partial()
http://pratapreddypilaka.blogspot.com/2011/11/htmlpartial-vs-htmlaction-mvc-razor.html is a great resource that explains Html.Action()
and how it differs from Html.Partial()