SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • Azure (0)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

Making Your Own Semantic Controls

(Apr 27 2007 - 06:02:26 PM by Timothy Khouri) - [print article]

ASP.NET Not Always Semantic

With the current thrust of the Internet moving towards "Semantic Markup" and clean HTML, you may begin to find that some ASP.NET controls that you are using render their HTML incorrectly such as a CheckBoxList using a <table> tag to render it's children, or even an ASP.NET AJAX UpdatePanel forcing either a <div> or <span> tag when you may want to render to a <label> or <fieldset> tag, etc..

While I always recommend using the .NET controls if they render semantically, I do also recommend overriding the default rendering in some cases to achieve clean, semantic markup. I'll demonstrate how to overcome the bad HTML rendering of the two above mentioned controls.

First we will make the SemanticUpdatePanel class. This class is exactly like the ASP.NET AJAX UpdatePanel that was made by Microsoft, but it adds a couple of features. The most important being the ability to choose what HTML tag should be rendered. Second, we will make our own SemanticCheckBoxList control that will render our checkboxes in a <ul> tag because it truthfuly is an "unordered list." Some people may want to argue that this is an "ordered list" and should therefore be rendered in a <ol> tag, but that's a different topic. First we should probably discuss briefly the issue.

What is Semantic Markup?

The word "semantic" means basically that something has "meaning." In the case of HTML tags, the "tag" has to represent the "content" inside of it. In other words, an <ol> tag (which stands for "Ordered List") should ONLY be used to represent an ordered list of data. If the data wasn't ordered (meaning it wasn't either in alphabetical order, or in numeric order etc.) then you would use ONLY a <ul> tag because that stands for "Unordered List."

Probably the most abused HTML tag would be the <table> tag, because people use it to layout their website visually, instead of using it to display only tabular data. This will be discussed more fully in an article in the "Standards" section of this site, but for now, on to the code!

By the way, for the "UpdatePanel" you need to have the ASP.NET AJAX DLL referenced in your web project.

using System;
using System.Web.UI;
using System.ComponentModel;

public class SemanticUpdatePanel : UpdatePanel
{
   // We are going to add only 1 property to the UpdatePanel.

   // This property will allow us to change how we want our

   // SemanticUpdatePanel will render.

   private HtmlTextWriterTag renderedElement = HtmlTextWriterTag.Div;

   public HtmlTextWriterTag RenderedElement
   {
       get
       {
           return this.renderedElement;
       }

       set
       {
           this.renderedElement = value;
       }
   }

   // Now we can simply override the "RenderChildren" method and

   // put our own custom logic in here to render the HTML tag

   // that we want to use.

   protected override void RenderChildren(HtmlTextWriter writer)
   {
       ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);

       // This code is here so that we only render the inner contents

       // of the control if it's being called asynchronously.

       if (scriptManager.IsInAsyncPostBack)
       {
           base.RenderChildren(writer);

           return;
       }

       // Of course we need to add our "ID" attribute.

       writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);

       // Here is where we render our own HTML element.

       writer.RenderBeginTag(this.renderedElement);

       // Let the UpdatePanel do it's own magic.

       base.Controls[0].RenderControl(writer);

       // And of course close off the tag.

       writer.RenderEndTag();
   }
}

That was very simple to do. But you might be wondering how I knew to override the "RenderChildren" method, not to mention how I knew that we need to check to see if we are in an async post-back and then adjust accordingly, right? Well the answer to that is simple, I used ".NET Reflector" created by Lutz Roeder to show me the code behind the UpdatePanel.

So now that we have the C# code, we need to see how easy it is to use this control in our HTML. Here it is:


<SingingEels:SemanticUpdatePanel ID="myUpdatePanel" runat="server" RenderedElement="Marquee">
   <ContentTemplate>
       Who uses a "Marquee" tag these days?
   </ContentTemplate>
</SingingEels:SemanticUpdatePanel>

So now we have the C# code and the ASP.NET code that would be needed to make our own semantic control. Do to time and space issues, I'll have to write another article to address the SemanticCheckboxList control.

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Check Out Dev++

Test your development skills, give proof to recruiters and employers at dev++

Related Blogs

These are the most recent blog posts related to this article.

  • Aspose.Newsletter February 2012 Edition is out Now
  • Embed Videos inside PowerPoint Presentation & Stable PDF Generation
  • Excel to PDF Conversion, XLS, XLSX & ODS Rendering are Improved
  • Convert SVG to Pdf & Render Pdf into Image with Desired Dimensions
  • Add Optical Character Recognition in Java Web & Desktop Applications

Related Ads

SingingEels.com as of Feb 04 2012 - 12:39:13 AM - (0.1406259)