|
How to globally register user and custom controls in ASP.NET 2.0
|
|
Rating: 13 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 8/8/2007,
in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 19846 times
Abstract: Abstract : In this short article, we will learn how to globally register user and custom controls in your web.config file. This saves us from repeatedly declaring the Register directive on every page where the control is being used.
How to globally register user and custom controls in ASP.NET 2.0
In previous versions of ASP.NET, if you wanted to import and use any user or custom control, you needed to add a page directive <%@ Register %> in that .aspx page. Shown below is an example of the register directive for a user control.
<%@ Register TagPrefix="uc" TagName="table" Src="table.ascx" %>
<body>
<form id="form1" runat="server">
<div>
<uc:table ID="Table1" runat="server" />
</div>
</form>
</body>
In ASP.NET 2.0, managing controls has become easier. Instead of declaring them on every page, you can declare them only once in your web.config file and use them in your entire project.
<configuration>
<system.web>
<pages>
<controls>
<addtagPrefix="uc"tagName="table"src="table.ascx" />
</controls >
</pages >
</system.web>
</configuration>
Once you have registered this control in your web.config, you can use this control on any page without explicitly adding a register directive on the page.
<body>
<form id="form1" runat="server">
<div>
<uc:table ID="Table1" runat="server" />
</div>
</form>
</body>
While using this control, you will also notice the intellisense support which saves us from remembering the syntax for using the control.
Well that’s it. It’s that easy. Thanks to the book MVP: Hacks and Tips book from Wrox which mentioned this tip.