Monday, March 14, 2011

How to register custom user controls in Web.Config

Let's say you have a Custom User Control (Test.ascx) that you want to register, but you dont want to register on each and every page. There are multiple ways of doing:

1. You can register in Master page, then any page that inherits the master page, can reference the User Control. That way you only need to register once and use anywhere/any-page you want.

2. If you dont have a Master page (which was my case, in the application I have working). You can register in web.config and use it anywhere/any-page you want. Configuring in web.config also helps when you have multiple master pages.

To configure custom user controls in Web.config, just add a new element under <pages><controls>

Example:

<system.web>
<pages>
<controls>
<add tagPrefix="UC" src"~/Controls/Test.ascx" tagName="Controls" />
</controls>
</pages>
</system.web>

Once registered, you can use it on every page. Going with the example above,

<UC:Controls ID="Test1" runat="server" />