Click here to Skip to main content
15,881,559 members
Articles / Web Development / HTML

Extending ASP.NET Web Controls With Custom HTML Attributes

Rate me:
Please Sign up or sign in to vote.
4.83/5 (37 votes)
13 Apr 20054 min read 234.7K   2.1K   115  
Use existing functionality in ASP.NET to extend the framework's web controls.
namespace htmlAttributes
{
	using System;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;

	/// <summary>
	/// Summary description for AttributeParser.
	/// 
	/// Used for processing the custom attributes specified in the 
	/// HTML part of a ASP.NET web page.
	/// </summary>
	public sealed class AttributeParser
	{
		private static string[] customAttributes;

		/// <summary>
		/// Static constructor
		/// </summary>
		static AttributeParser() 
		{
			customAttributes = Enum.GetNames(typeof(AttributeType));
		}

		/// <summary>
		/// Gets the names of the currently defined custom attributes.
		/// </summary>
		public static string[] Attributes
		{
			get { return customAttributes; }
		}

		/// <summary>
		/// Processes the control's custom attributes.
		/// </summary>
		/// <param name="control">Current web control being rendered.</param>
		public static void ProcessControl(WebControl control)
		{
			foreach(string attribute in Attributes)
			{
				// Get the value of the custom attributes
				string attributeValue = control.Attributes[attribute];

				// If the custom attribute is defined, process it
				if (attributeValue != null)
				{
					// Get the attribute's type
					AttributeType at = (AttributeType)Enum.Parse(typeof(AttributeType), attribute, true);

					// Get the class to process the custom attribute
					IAttribute customAttribute = AttributeFactory.GetAttribute(at);

					if (customAttribute != null)
					{
						// Apply the attribute for the control
						customAttribute.Apply(attributeValue, control);
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Javier is a consultant specializing in ASP.NET, system architecture, and training. He's also a MCSD .NET and a co-founder of the Iowa DotNET Users Group. Recently, Javier was awarded the Visual Developer ASP.NET MVP award from Microsoft.

On his blog you can find posts on ASP.NET, architecture, design patterns, XML and just recently, Indigo. Javier enjoys giving back to the community by speaking at user groups, local/regional .NET events, being active in forums and by writing articles for Code Project.

In his spare time, Javier enjoys spending time with his wife, newborn son, two golden retrievers and writing about himself in third person.

Click here to read Javier's blog.

Comments and Discussions