ASP.net Tip: Register User/Custom Controls globally



Generally when adding/dropping an user/custom control to a page, we use/used to use the @Register directive to reference our controls (or the IDE did it for you), like this:
 
...
<%@ Register Src="Controls/SomeControl.ascx" TagName="SomeControl" TagPrefix="cstruter" %>
<%@ Register Assembly="ServerControl" Namespace="ServerControl" TagPrefix="somevendor" %>
...
<body>
    <form id="form1" runat="server">
    <div>
        <cstruter:SomeControl ID="SomeControl1" runat="server" />
        <somevendor:ServerControl1 ID="ServerControl11" runat="server" />
    </div>
    </form>
...
 

Now this is bit of a dodgy situation, since whenever we want to use these controls we need to add the directive above each and every page - which obviously is a bit of a bad idea.

It would make a lot more sense to register these controls somewhere globally - which is exactly what we can do since ASP.net 2.0, observe:
 
<system.web>
...
    <pages>
      <controls>
	  ...
        <add src="~/Controls/SomeControl.ascx" tagName="SomeControl" tagPrefix="cstruter" />
        <add assembly="ServerControl" namespace="ServerControl" tagPrefix="somevendor" />
...
 

Which means we can simply remove the @Register directives from our pages, since the references to our controls are available via the web.config now.

Note: If you look closely you will notice I added the tilde sign infront of the path where the usercontrol in the example resides - which resolves the path to the root of the web application.







Post comment

Name *
Email
Title
Body *
Security Code
*
* Required fields

Related Posts

ASP.net (C#) - WebControlAdapter


2010-03-11 21:47:12

Encrypting your web.config


2008-06-18 07:42:23

Latest Posts

Be the best stalker you can be


2011-12-13 22:33:54

Syntactic sugar (C#): Enum


2011-08-04 16:50:18

Top 5 posts

Simple WYSIWYG Editor


Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

Moving items between listboxes in ASP.net/PHP example


Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
2008-06-12 17:07:43

Cross Browser Issues: Firefox Word Wrapping


Firefox word wrapping issues
2008-06-09 09:51:21

Populate a TreeView Control C#


Populate a TreeView control in a windows application.
2009-08-27 16:01:03

C# YouTube : Google API


Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51