Tip #9 Did you know … You can also register your ASP.NET Web User Controls in Web.config?

In Tip#8 we saw how to create a Web User control, register a Web User Control and use the Web User control in your page. Note that VS added a @Register directive right at the top of the page on drag drop of the user control on to your page.

But in future if you want to move the location of your User controls you will be forced to change the Register directive in multiple places depending on how many pages you have used the User Control. This could be a pain. This can be easily solved by moving the registration of the user control to your Web.config file with the following entry, so that any change in location can be updated only at one place.

 Web config entry would look like:
 <configuration>
<system.web>
<pages>
 <controls>
<add tagPrefix="MyControl" src="~/Controls/WebUserControl.ascx"
 tagName="MyButton"/>
</controls>
 </pages>
</system.web>
</configuration>
 Once the web config entry is made. The control can then be used in any page like this:
 <body>
    <form id="form1" runat="server">
        <MyControl:MyButton ID="MyUserControl" runat="server" />
    </form>
</body> 

Reshmi Mangalore

SDET, Web Development Tools

Comments

  • Anonymous
    September 15, 2008
    PingBack from http://hoursfunnywallpaper.cn/?p=6236

  • Anonymous
    September 15, 2008
    The comment has been removed

  • Anonymous
    September 15, 2008
    The comment has been removed

  • Anonymous
    September 29, 2008
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    November 05, 2008
    ascx control and page/control that is using the control can't live in same directory. if so throws err "because it is registered in web.config and lives in the same directory as the page." works after moving control/page to different directory or sb told me if you register control again on page.

  • Anonymous
    June 18, 2009
    原文地址:HowtocreateanASP.NETWebUserControlandincludeitinyourwebpage在上一篇创建web用户控件并包含在web页...