Welcome to MSDN Blogs Sign in | Join | Help

News

  • These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified Terms of Use
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

Posted: Monday, September 15, 2008 11:31 PM by WebDevTools

Comments

Visual Web Developer Team Blog said:

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

# September 15, 2008 7:48 PM

Visual Web Developer Team Blog said:

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

# September 15, 2008 7:50 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# September 29, 2008 10:10 PM

web developer said:

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.

# November 5, 2008 2:29 PM

xjb said:

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

# June 18, 2009 9:19 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker