Using Tag Mapping in ASP.NET 2.0
Provided by: Dave Juth, Senior Systems Architect
Tag mapping allows you to swap compatible controls at compile time on
every page in your web application. A useful example is if you have a stock
ASP.NET control, such as a DropDownList, and you want to replace it with a
customized control that is derived from DropDownList. This could be a
control that has been customized to provide more optimized caching of lookup
data. 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:
<pages>
<tagMapping>
<clear
/>
<add
tagType="System.Web.UI.WebControls.DropDownList"
mappedTagType="SmartDropDown"/>
</tagMapping>
</pages>
When you run your application, ASP.NET will replace every instance of
DropDownList with SmartDropDown, which is derived from DropDownList:
public
class
SmartDropDown
: DropDownList
{
public
SmartDropDown()
{
}
// ... etc.
}
ASP.NET will throw a compile error if you specify controls that are not
compatible:
<pages>
<tagMapping>
<clear
/>
<add
tagType="System.Web.UI.WebControls.DropDownList"
mappedTagType="
System.Web.UI.WebControls.Label"/>
</tagMapping>
</pages>

Tag mapping is a very effective way of implementing custom controls in
your ASP.NET application, or for quickly testing custom controls. Make sure
your mapped type is compatible with the type it will replace and you can
make your web application development much easier by using this new feature
of ASP.NET 2.0.
Return to the tips page
|