我有一个名为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

有帮助吗?

解决方案

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.
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top