Register Custom Controls In Web.config

This one is probably old news to many of you, but I just recently ran across it. Every time I want to add a new control to a new page, I get annoyed because I have to remember that annoying syntax for registering a control.

Let’s see...how does it go again? Do I have to add a TagName attribute? No, that’s for user controls. Hmmm, forget it, I’ll just dynamically add it! Well in the interest of reducing future angst, here are two examples of the syntax, one for a custom control and one for a user control.

<%@ Register TagPrefix="st" Namespace="Subtext.Web.Controls" 
  Assembly="Subtext.Web.Controls" %>
<%@ Register TagName="SomeControl" TagPrefix="st" 
  Src="~/Controls/SomeControl.ascx" %>

The first one registers the tag prefix st with the Subtext.Web.Controls namespace in the Subtext.Web.Controls assembly. The second one registers the tag name SomeControl with the user control SomeControl.ascx

Add this to the top of your page or user control and you can reference a control from this assembly like so:

<st:HelpToolTip id="blah" runat="server" HelpText="Blah!" />
<st:SomeControl id="foo" runat="server" />

A most helpful tooltip!

Fortunately, starting with ASP.NET 2.0, we can register a tag prefix within the Web.config file. This basically makes all the controls within that namespace and assembly available to all pages without having to add that ugly Register declaration.

<system.web>
    <pages>
      <controls>
        <add assembly="Subtext.Web.Controls"
                namespace="Subtext.Web.Controls"
                tagPrefix="st" />
        <add src="~/Controls/SomeControl.ascx"
                tagName="SomeControl"
                tagPrefix="st" />
      </controls>
    </pages>
</system.web>

Thanks to the ASP.NET 2.0 MVP Hacks book for this one.

What others have said

Requesting Gravatar... jayson knight Nov 14, 2006 11:45 PM
# re: Register Custom Controls In Web.config
Doh! I remember reading about this a while back but completely forgot about it...thanks for the reminder!
Requesting Gravatar... Dave Nov 15, 2006 11:19 AM
# re: Register Custom Controls In Web.config
That's awesome! The fewer things in the front end the better!
Requesting Gravatar... Rick Strahl Nov 15, 2006 1:38 PM
# re: Register Custom Controls In Web.config
This is a great feature yeah, but be aware that currently it doesn't work with Web Application Projects, so if you move a project over all of a sudden you may be missing @Register tags...
Requesting Gravatar... Christopher Steen Nov 15, 2006 7:08 PM
# Link Listing - November 15, 2006
Client-Side Enhancements in ASP.NET 2.0 [Via: ] Programmatically adding pages to a MOSS Publishing site...
Requesting Gravatar... Haacked Nov 16, 2006 4:44 PM
# re: Register Custom Controls In Web.config
Actually, it does work with the latest Web Application Projects! I just tried it. Woohoo! [:)]
Requesting Gravatar... ScottGu's Blog Nov 26, 2006 12:57 PM
# Tip/Trick: How to Register User Controls and Custom Controls in Web.config
I've been including this technique in my ASP.NET Tips/Tricks talks the last year, but given how many
# Tip/Trick: How to Register User Controls and Custom Controls in Web.config
I've been including this technique in my ASP.NET Tips/Tricks talks the last year, but given how many
Requesting Gravatar... Top ASP.NET Items Nov 26, 2006 1:27 PM
# Tip/Trick: How to Register User Controls and Custom Controls in Web.config
I've been including this technique in my ASP.NET Tips/Tricks talks the last year, but given how many
Requesting Gravatar... Cyokin Dec 03, 2006 2:12 AM
# re: Register User Controls In Web.config
<pages>
<controls>
<add tagPrefix="miles" src="~/Controls/Header.ascx" tagName="header"/>
<add tagPrefix="miles" src="~/Controls/Footer.ascx" tagName="footer"/>
</controls>
</pages>
Requesting Gravatar... ASP.NET Chinese Blogs Dec 13, 2006 2:09 PM
# 技巧和诀窍:如何在Web.config中注册用户控件和自定义控件
【原文地址】 Tip/Trick: How to Register User Controls and Custom Controls in Web.config 【原文发表日期】 Sunday, November
Requesting Gravatar... freeliver54 Jan 08, 2007 4:42 PM
# [转]技巧和诀窍:如何在Web.config中注册用户控件和自定义控件
本文转自http://blog.joycode.com/scottgu/archive/2006/11/27/88083.aspx其文如下技巧和诀窍:如何在Web.config中注册用户控件和自定...
Requesting Gravatar... jobin Aug 24, 2007 1:31 AM
# Problem in adding control to web Form
sir,

iam using a third party progress bar control.i refer tht control to my project.bt when i drag tht control to my form the following error is displayed "An Unhandled Exception Occured.Request is not available in this context".I don't know how it happen.what is the reason for this?
Requesting Gravatar... Rod Smith Nov 25, 2007 7:55 PM
# re: Register Custom Controls In Web.config
Hi,

I've come across a problem with this. Basically, it doesn't work. User controls registered in the web.config are not being picked up by any of the pages. However, there are a few interesting factors. Firstly, I'm working in a development team, and only half of us are having this problem, even when working on the same project. Also, user controls that have been registered on a machine where it works are then fine on my machine, until I make a change to them.

I don't know if you've seen anything like this. I've trawled the internet and found a number of people who have had this problem, but no-one's provided a solution. Any ideas would be greatly appreciated.

Thanks,
Rod.
Requesting Gravatar... Skelly Dec 07, 2007 10:01 AM
# re: Register Custom Controls In Web.config
Thanks! I was pulling my hair out trying to globally reg a user control that was not in app_code -- then I found your post.
Requesting Gravatar... Marcos Accioly Apr 24, 2008 8:18 PM
# re: Register Custom Controls In Web.config
Thanks mate... It sounds stupid (how could I not think of that?? :-) at first but it is a hell of a hint.

Cheers from Brazil!
Requesting Gravatar... Bruce Quackenbush May 26, 2008 12:35 PM
# re: Register Custom Controls In Web.config
Might be just me but I chased my tail on this for longer than I care to admit, so I thought I'd share.

The tag attribute names are case sensitive in web.config! Specifically:
src
assembly
namespace
tagPrefix

Miss that upper-case P and it doesn't work at all.
Requesting Gravatar... greg Jul 11, 2008 3:32 AM
# re: Register Custom Controls In Web.config
cannot use the user control because it is registered in web.config and lives in the same directory as the page
Requesting Gravatar... Matt Aug 21, 2008 5:04 AM
# re: Register Custom Controls In Web.config
I have two web.config files. One in a subdirectory because i plan on deploying the entire subdirectory as a product on its own and one in the root of our corporate site. When having these tags in the second web.config file in our subfolder this functionality is failing. It just says "The control cannot be displayed because its tagprefix is not registered on this page" even though all controls are registered in the web.config
Requesting Gravatar... Matt Sep 12, 2008 1:55 AM
# re: Register Custom Controls In Web.config
Does anyone know how to register the control in the web.config as described but then actually add the control at runtime from the code behind.

I've done this before when the control is part of the project or a reference has been set (server control) with code like this:

Dim mycontrol As MyControl
mycontrol = New ChartElement
mycontrol = Page.LoadControl("MyControl.ascx")
Page.Controls.Add(mycontrol)

But the code understandably doesn't know what the controls are without some sort of reference.

I hoping to create an application where my customer can build their own controls, place them in a directory, register the control in the web.config and then the app will automatically add the control to the page.
I know how to do this using webparts but I need to avoid that for the time being.
Is Reflection the answer? I'm not currently familiar with that.
Requesting Gravatar... Ahmed Eltawil Nov 04, 2008 9:27 AM
# re: Register Custom Controls In Web.config
I was looking for the syntax you provided. Thanks!
Requesting Gravatar... Mark Kamoski Dec 03, 2008 12:09 PM
# re: Register Custom Controls In Web.config
How can I register a custom server control in web config and use it both (1) declartively in the code-infront of an ASPX page as well as (2) use it as a class library in a .CS file in App_Code? I can do (1) but I cannot seem to do (2). Do you have any hints? Please advise. Thank you. -- Mark Kamoski
Requesting Gravatar... Walter Dec 31, 2008 1:22 PM
# re: Register Custom Controls In Web.config
Hey Thanks! It works!
Requesting Gravatar... Leo Sutherland Mar 27, 2009 8:58 AM
# re: Register Custom Controls In Web.config
<st:HelpToolTip id="blah" runat="server" HelpText="Blah!" />
<st:SomeControl id="foo" runat="server" />

Great tip as far as it goes - I need the syntax for using a custom component on a web page. Process of elimination means the second of the two lines above is for the User Control, therefore the first is for the Custom Componenet.

But where the "HelpToolTip" come from - what it refer to? THe test Custom control I'm playing with has a Text Property and a Render Property - should I be doing something like

<st:Text id="blah" runat="server" HelpText="Blah!" />

or perhaps

<st:?????? id="blah" runat="server" Text="Blah!" />

where I don't know what ?????? is

or even

<st: id="blah" runat="server" Text="Blah!" />

Please help - me confused :)


Requesting Gravatar... Ryan May 07, 2009 7:29 AM
# re: Register Custom Controls In Web.config
Hi,
Do you how to do for IIS 7?
how to do it inside <system.webserver> tag?

What do you have to say?

(will show your gravatar)
Please add 7 and 5 and type the answer here: