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 (6)

  1. Here are three new tips on Tips and Tricks Blog. Tip #7 Did you know… How to Sort data using a ListView

  2. Here are three new tips on Tips and Tricks Blog. Tip #7 Did you know… How to Sort data using a ListView

  3. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  4. web developer says:

    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.

  5. xjb says:

    原文地址:HowtocreateanASP.NETWebUserControlandincludeitinyourwebpage在上一篇创建web用户控件并包含在web页…

Skip to main content