2009-10-27 109 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. 
    } 
}