ASP.NET and ASP.NET 2.0 FAQ (Answers to Frequently Asked Questions)

Question: How do I register a User Control once for an entire application?

Answer: You can register a User Control in the Web configuration (Web.config) file and use the User Control within any ASP.NET page within the same application.

Normally, whenever you need to use a User Control in an ASP.NET page, you must first register the User Control with the Register %> directive. However, you can avoid registering the User Control in individual pages by registering the User Control in the Web configuration (Web.config) file. This is a new feature of the ASP.NET 2.0 Framework.

For example, imagine that you have created a User Control named RandomQuote.ascx located in a folder named UserControls. You can register the User Control for all the pages in an application with the following Web configuration file.

Be aware that there is one important limitation when registering User Controls in the Web.config file. When a User Control is registered in the Web.config file, you cannot place the User Control in the same folder as a page that uses the User Control. To work around this limitation, you should always place all of your User Controls in a separate folder. I typically create a User Controls folder for each of my web applications.