Tag mapping in ASP.NET

Yesterday while I was going through the web config, I came across a section which tempted me to explore it further .

The section is ” ” inside , inside . Now allow me share, what I learnt. I think its little known and rarely used feature of ASP.NET. Rarely used because there’s only a specific situation where you’d need it, but when you need it, it’s so handy.

Basically, it’s a way to turn (or swap) all instances of a type into another type at compile time. For example if we have used, lets say System.Web.UI.WebControls.DropDownList  then it can turn all instances in the entire website into another compatible control that has been customized, by compatible control i mean a custom control derived from System.Web.UI.WebControls.DropDownList, ofcourse, ASP.NET will throw a compile error if you specify controls that are not compatible.

Now, suppose I am having System.Web.UI.WebControls.DropDownList in many web forms, instead of editing every web form and replacing the built in DropDownLists with your custom version, you can have ASP.NET in effect do it for you by modifying web.config:

  
    
    
  


When you run your application, ASP.NET will replace every instance of DropDownList with CustomDropDown, which is derived from DropDownList:

public class CustomDropDown : DropDownList
{
    public CustomDropDown()
    {
       // Code
    }
}

That is one smart way of applying your own server control substitute classes on a site wide basis.

Hope that you enjoyed learning it.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!