Globally register User and Custom Controls in ASP.NET

Dec 8, 2009 Posted by Lara Kannan

In previous versions of ASP.NET, to import and use user or custom controls on a page, you needed to add a page directive (<%@ Register %>) in the .aspx code for that page.

Fortunately, ASP.NET 2.0 and later versions introduced a shortcut that makes managing the controls much easier. Rather than declaring them on every page, you can declare them once, in your web.config file, and then use them in your entire project.

Register your controls in web.config as shown below. The example assumes that there is a user control named table.ascx in the project:


<configuration>
<system.web>
<pages>
<controls>
<addtagPrefix="uc" tagName="table" src="table.ascx" />
<!-- Add other user/custom control references here -->
</controls >
</pages >
</system.web>
</configuration>

You can now use the table.ascx user control on any page—without explicitly adding a Register directive on the page. Here's a short example:


<body>
<form id="form1" runat="server">
<div>
<uc:table ID="Table1" runat="server" />
</div>
</form>
</body>

Thanks : devx.com

I hope this will help you. A 'Thank You' would be nice!

Share
Labels: ,
  1. Anonymous

Post a Comment