ASP.NET Web Forms Extensibility: Tag Mapping

There may be times when you want a particular setting applied to all controls of a given type, in all pages. Or you want to debug this control, but you don’t have access to it’s source code. Or you want to change its behavior. For that, you can use tag mapping, and I have given an example before.

In a nutshell, ASP.NET lets you change the control that would normally be instantiated by some server tag (like <asp:Image />, <asp:TextBox />, etc) for your own control, which must inherit from the class of the original control. This means that on all places that you have declared, for example, an <asp:TextBox />, instead of the standard TextBox, ASP.NET will instead place an instance of your own class, where you can override methods, set property defaults on its constructor or place breakpoints.

Usage is very simple, just add entries to the tagMapping section:

   1: <tagMapping>
   2:     <add tagType="System.Web.UI.WebControls.TextBox" mappedTagType="MyNamespace.MyTextBox, MyAssembly"/>
   3: </tagMapping>

Remember, MyTextBox must inherit from TextBox, or you will get an exception.

All of the properties that are present on markup will be passed to the new control, skins will also apply. In case you are wondering, this won’t work with web user controls (those that have an .ASCX file).


No Comments