ASP.NET - Dynamic Control Mapping

by nmgomes

I already posted here about Tag Mapping and how helpful it can be, but naturally there's are a few improvements that I would like to see available in future framework release.

The one I expect the most is about the capability of mapping dynamic created controls using the same rules as Tag Mapping uses when interpreting the page markup.

Without this capability we can never use widely the tag mapping because whenever we need to create dynamic controls they will be strongly coupled to a specific control implementation.

Imagine this scenario:

  1. First you have built an web application that use standard ASP.NET TextBox  control, some of them dynamically created.
  2. Now, imagine that you want to reuse that application, as is, but instead of ASP.NET Textbox control you want to use your own Textbox implementation.

This task could be easily accomplished using Tag Mapping if no dynamic controls were used, but in this scenario ASP.NET give us no solution, so the application cannot be reused without modifications.

Naturally, you can copy/paste your application and make the necessary changes, or even add a few if statements, but that will only increase complexity and maintenance effort.

Until the .NET team provide us such capability we must do the magic ourselves.

My proposal is an help class (DynamicControlBuilder) that provide us two methods: GetMappedType and CreateControl.

/// <summary>/// Gets the mapped <see cref="System.Web.UI.Control"/> type./// </summary>/// <param name="type">The <see cref="System.Web.UI.Control"/> type to be mapped</param>/// <param name="prefix">The namespace prefix.</param>/// <returns>A <see cref="System.Type"/> object.</returns>public static Type GetMappedType(Type type, string prefix){    if (!typeof(Control).IsAssignableFrom(type))    {        throw new ArgumentOutOfRangeException("type", "Must inherit from Control.");    }    Type mappedtype;    if (!string.IsNullOrEmpty(prefix))    {        TagPrefixInfo prefixinfo;        if (!m_prefixes.TryGetValue(prefix, out prefixinfo))        {            throw new ArgumentException("prefix", "No prefix found.");        }        else        {            type = BuildManager.GetType(string.Format("{0}.{1}, {2}", prefixinfo.Namespace, type.UnderlyingSystemType.Name, prefixinfo.Assembly), false);            if (type == null)            {                throw new ArgumentException("type", "Control not found within specified prefix.");            }        }    }    if (m_tagMappings.TryGetValue(type.UnderlyingSystemType, out mappedtype))    {        return mappedtype;    }    return type;}/// <summary>/// Creates a dynamic mapped <see cref="System.Web.UI.Control"/>./// </summary>/// <param name="type">The <see cref="System.Web.UI.Control"/> type to be mapped</param>/// <param name="prefix">The namespace prefix.</param>/// <returns>A <paramref name="T"/> object.</returns>public static Control CreateControl(Type type, string prefix){    Type mappedType = GetMappedType(type, prefix); ;    return (Control)Activator.CreateInstance(mappedType);}

The main goal is to enable any of the following usages:

this.Page.Controls.Add(DynamicControlBuilder.CreateControl<System.Web.UI.WebControls.TextBox>("foo"));this.Page.Controls.Add(DynamicControlBuilder.CreateControl(typeof(System.Web.UI.WebControls.TextBox), "foo"));this.Page.Controls.Add(DynamicControlBuilder.CreateControl(typeof(System.Web.UI.WebControls.TextBox)));

Try it !

DynamicControlBuilder.cs (7.13 kb)

kick it on

Filed in: .NET | ASP.NET | CodeProject


.NET Framework 2.0 SP1 - TagMapping Undocumented hotfix

by nmgomes

While searching a way to solve the HtmlControls tag mapping problem I found that the new SP1 bring an extra undocumented hotfix.

This undocumented hotfix changes MainTagNameToTypeMapper.GetControlType method so that, for every tag name regardless is kind, a tag type mapping is always performed.

This little change enables us to tag map HtmlControls the same way we map WebControls and UserControls. Here is an example: [more]

<system.web> <pages> <tagMapping> <add tagType="System.Web.UI.HtmlControls.HtmlForm, System.Web, Version=" mappedTagType="tagmapping.controls.Form, tagmapping" /> </tagMapping> </pages> </system.web>

This tag mapping problem is also fixed for .NET Framework 3.5.

For those of you who still use .NETFX 2.0 without SP1 the only solution I found available is:

<system.web> <pages> <controls> <add tagPrefix="html" namespace="System.Web.UI.HtmlControls" assembly="System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </controls> <tagMapping> <add tagType="System.Web.UI.HtmlControls.HtmlForm, System.Web, Version=" mappedTagType="tagmapping.controls.Form, tagmapping" /> </tagMapping> </pages> </system.web>

Then you should replace the <form> tag with an <html:HtmlForm> tag in your pages.

kick it on

Filed in: .NET


ASP.NET - Tag Mapping

by nmgomes

Tag Mapping is not a new ASP.NET 2.0 feature, but only recently many people discover it.

      <add tagType="System.Web.UI.WebControls.TextBox"

The use of Tag Mapping allow us to, at parsing time, replace a given control type with other control type. The only constrain is that new control type extends primary control type.

If I use Tag Mapping with Web Controls everything works fine but if, instead, I use Html Controls, like HtmlForm, with tag mapping no mapping is done.

Using Reflector I found no reason for this behavior.

Filed in: ASP.NET