代码之家  ›  专栏  ›  技术社区  ›  ETFairfax

HTMLHelper扩展-用户控制

  •  0
  • ETFairfax  · 技术社区  · 15 年前

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

    非常感谢你的帮助。

    埃特费尔法克斯

    1 回复  |  直到 12 年前
        1
  •  1
  •   Matthew    15 年前

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