Register User Controls and Custom Controls in Web.config

Thursday, October 15, 2009

Problem with declaring usercontrols in all page

<%@ Register TagPrefix="rajesh" TagName="Header" Src="UserControls/Header.ascx" %>
<%@ Register TagPrefix="rajesh" TagName="Footer" Src="UserControls/Footer.ascx" %>
<%@ Register TagPrefix="Control1" Assembly="Control1" %>

<form id="form1" runat="server">
<rajesh:Header ID="rajeshHeader" runat="server" />

This works fine, but suppose you have so many user control and you are using them in all pages by simply dragging and dropping .

so if at some time ,you have changed the path of user control .

then you need to change the path of user control in all pages in which you have used user control.

Solution to the problem discussed above

The above situation might not have occurred if you have registered the user control in web.congif file.

Instead of declaring and duplicating in all pages, its better to declare once in web.config file and use as many time you want to use in your page.

<?xml version="1.0"?>



<add tagPrefix="rajesh" src="~/UserControls/Header.ascx" tagName="header"/>
<add tagPrefix="rajesh" src="~/UserControls/Footer.ascx" tagName="footer"/>
<add tagPrefix="Control1" assembly="Control1"/>



Once you declare the UserControls within the web.config file, you can then just use the controls on any page.

<form id="form1" runat="server">
<rajesh:header ID="rajeshHeader" runat="server" />

Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)

No comments :