|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHave you ever written code to implement custom functionality, (text formatting, permission checks, etc.), to existing ASP.NET controls? You can achieve such customizations by implementing custom web controls, but wouldn't it be nice to apply extra attributes to existing ASP.NET controls to achieve the same result? This article covers a way you can use existing functionality in ASP.NET to extend existing web controls by applying your own custom HTML attributes. A Quick BackgroundTo understand this extended functionality, one must be familiar with the ASP.NET life-cycle, particularly the way a Creating A Simple Custom PageTo implement a simple base ASP.NET page, define a new class that inherits from System.Web.UI.Page. public class BasePage : System.Web.UI.Page { ... } Next we need to override the protected override void CreateChildControls() { ... } Since all web and user controls must reside in a HTML form in order to work, we need to do two things:
The following code defines this discovery process: /// <summary> /// Overrides the common functionality for the creation /// of controls within the page. /// </summary> protected override void CreateChildControls() { // Find the form control in which all asp.net (or custom) controls // must be hosted. foreach (Control c in this.Controls) { if (c is System.Web.UI.HtmlControls.HtmlForm) { ProcessForm(c as HtmlForm); } } // Call the base implementation base.CreateChildControls(); } /// <summary> /// Process each of the controls within the form /// </summary> /// <param name="form">The html form for the page</param> private void ProcessForm(HtmlForm form) { // Find all the web controls, ie <asp:control_name />, and // check process them. // // You can also add logic here to check for user controls or // custom user controls. foreach (Control cc in form.Controls) { if (cc is WebControl) { // Once you find the a web control, process any // custom attributes you have specified. AttributeParser.ProcessControl(cc as WebControl); } } } Now that we have our base page and we've defined a discovery process, we can start the handling of our custom attributes with the help of some extra classes. Stepping Through The CodeThe main class for discovering custom HTML attributes is the Here's a quick look at the code: /// <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); } } } } Two classes that implement the <asp:textbox id="txtData" runat="server" permission="Administrators">INFO</asp:textbox> Here's what the
The <asp:textbox id="txtPhone" runat="server" textstyle="phone" /> This
The /// <summary> /// Summary description for TextStyleAttribute. /// /// Contains the custom implementation for processing the style type /// for a System.Web.UI.WebControls.TextBox. /// </summary> public class TextStyleAttribute : IAttribute { #region IAttribute Members /// <summary> /// Applies the specified style to the web control. /// </summary> /// <param name="attributeValue">The style used for the rendered control</param> /// <param name="wc">The current control</param> public void Apply(string attributeValue, WebControl wc) { // Perform only if the control is a text box if (wc is TextBox) { // Get the style to apply for the text box TextStyle mode = (TextStyle)Enum.Parse(typeof(TextStyle), attributeValue, true); // Apply the specific style switch(mode) { case TextStyle.Phone: PhoneStyle(wc as TextBox); break; // Add more styles here. } } } #endregion /// <summary> /// Applies the 'Phone' style to the text box (ddd-ddd-dddd) /// </summary> /// <param name="text">Current TextBox control</param> private void PhoneStyle(TextBox text) { // Add custom javascript text.Attributes.Add("onKeyUp", "javascript:AutoPhone(this)"); // Set the max size for a phone number text.MaxLength = 12; } } For more information on the classes used, see the attached source. ConclusionThe two presented attribute implementations are intended to show you how easy it is to extend the current controls bundled with ASP.NET and to give you a head start for creating your own custom HTML attributes. If you currently have a custom ASP.NET framework adding this type of functionality will make your framework a bit more customizable. Please feel free to leave any comments below.
|
||||||||||||||||||||||