Question

J'ai un contrôle utilisateur personnalisé appelé ErrorNotificationBox. Pour placer cela sur ma page, je fais le vieux ...

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

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

Est-il possible d'étendre HtmlHelper pour inclure mon contrôle utilisateur? Ie ...

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

Merci beaucoup pour votre aide.

ETFairfax

Était-ce utile?

La solution

La plupart des méthodes de l'aide HTML sont des méthodes d'extension. Vous pouvez facilement ajouter le vôtre.

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.
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top