HtmlHelper Extension - User Conrol
-
06-07-2019 - |
Question
I have a custom user control called ErrorNotificationBox. To place that on my page I do the old...
<%@ Register Src="~/PathTo/ErrorNotificationBox.ascx" TagName="ErrorNotificationBox" TagPrefix="uc" %>
<uc:ErrorNotificationBox Runat="server" id="myEnb" Caption="My Test Caption" />
Is it possible to extend HtmlHelper to include my user control? I.e...
<%= Html.ErrorNotificationBox("My Test Caption")%>
Many thanks for any help.
ETFairfax
Solution
Most of the methods in html helper are Extension methods. You can easily add your own.
public static class MyExtensions
{
public static string ErrorNotificationBox(this HtmlHelper helper, string caption)
{
//generate and return the html for your error box here.
// loading and returning an instance of a user control (.ascx)
// as a string is difficult. You may find it easier to
// create the html with a string builder.
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow