2009-10-27 107 views
0

我有一个名为ErrorNotificationBox的自定义用户控件。为了把我的网页上我的老...HtmlHelper扩展 - 用户控制

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

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

是否有可能延长的HtmlHelper,包括我的用户控件? I.e ...

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

非常感谢您的帮助。

ETFairfax

回答

1

大多数的HTML辅助方法是扩展方法。您可以轻松添加自己的。

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