Расширение HtmlHelper - пользовательский контроль - PullRequest
0 голосов
/ 27 октября 2009

У меня есть пользовательский элемент управления с именем ErrorNotificationBox. Чтобы разместить это на своей странице, я делаю старую ...

<%@ Register Src="~/PathTo/ErrorNotificationBox.ascx" TagName="ErrorNotificationBox" TagPrefix="uc" %>

<uc:ErrorNotificationBox Runat="server" id="myEnb" Caption="My Test Caption" />

Можно ли расширить HtmlHelper для включения моего пользовательского элемента управления? Т.е. ...

<%= Html.ErrorNotificationBox("My Test Caption")%>

Большое спасибо за любую помощь.

ETFairfax

1 Ответ

1 голос
/ 27 октября 2009

Большинство методов в html helper являются методами Extension. Вы можете легко добавить свой собственный.

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