How to Register User Controls and Custom Controls in Web.config to manage them easily

Hi

In asp.Net 1.X we had to import and use both customs server controls and user control on a page by adding the @Register directives to the top of the page. Once registered developers could then declare these controls anywhere on the page using the tag prefix and tag names configured in the @Register directive.

This is fine but if we have too many user controls across the sites (and that too ascx files) then it can be painful to manage across the site.

The control declaration is much cleaner and easier to manage in Asp.Net 2.0. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file of your application

The controls need to be added in the controls tag inside the pages tag which will be inside the system.web tag.

<system.web>
    <pages>
      <controls>        <add tagPrefix="Vikram" src="~/Controls/VikFooter.ascx" tagName="footer"/>      </controls>
    </pages>
</system.web>

An important this to note here is to use the ~ path as the user control can be used anywhere in the site. The “~” will resolve the control from the root of the web site.

Once the control is registered in the web.config file the control can be used by any page or user control in the site.

Another Important thing to note is that there is no performance difference in either registering the controls in the web.config or on the top of the page as they get compiled down to the same instruction in both the scenarios.

Hope this helps
Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 11/27/2008 2:20:53 AM

"An important this to note here is to use the ~ path as the user control can be used anywhere" - i dont agree with this because i face issues when using the user controls inside another user control.
My senario is i have user controls in a folder called"USERCONTROLS" and the webpages in the main folder.

so the issue arises when i try to use usercontrol inside another user control.

hope i am clear

regards
rama charan

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia