Click here to Skip to main content
15,882,017 members
Articles / Web Development / ASP.NET

ASP.NET - Dynamic Control Mapping

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Apr 2010CPOL1 min read 10.6K   5  
ASP.NET - Dynamic Control Mapping

I already posted here about Tag Mapping and how helpful it can be, but naturally there are a few improvements that I would like to see available in the 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 a web application that uses 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 gives 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 provides us with such capability, we must do the magic ourselves.

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

C#
/// <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:

C#
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!

kick it on DotNetKicks.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect everis
Portugal Portugal
Over 13 years of experience in the Software Development working mainly in the banking and insurance industry.

Over 3 year of experience as Operations Team Leader focused on Infrastructure Management and Software Configuration Management.

I've been honored with the Microsoft Most Valuable Professional (MVP) Award for three consecutive years, 2010, 2011 and 2012, in recognition to exceptional technical contributions and leadership.

Current / Recent Technical Projects
- Dominican Republic Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Colombian SECOPII Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Main Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Development ecosystem management, including Server management, , capacity management, SW monitoring and deploy management

Areas of Specialization:
- Operations Management - ISO 20000 & ISO 27001 driven
- Team Management and Coaching
- Technology Leadership, Solutions/Architecture
- Product life cycle management, Continuous Integration
- Technological background in Microsoft frameworks and tools.

Comments and Discussions

 
-- There are no messages in this forum --