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

Semantic CheckBoxList & RadioButtonList

(May 08 2007 - 09:37:52 PM by Timothy Khouri) - [print article]

Quick Semantics Lesson

In an earlier article (Making Your Own Semantic Controls) we talked about how HTML tags should have a "meaning", and should define your content. An <ol> tag describes an ordered list of items, such as your "top 10 favorite sites." Because that list is in a specific order, you would use the <ol> tag. A <ul> tag describes an un-ordered list of items, such as your site's navigation links, or as in the case with this article, a list of radio buttons or checkboxes.

The purpose of using semantic HTML is to seperate your "content" from your code. HTML tags are used to tell a web browser, an audio browser or a search engine bot exactly "what" your content is. The problem with developers, especially old-school web designers, is that they rely on HTML tags to make something look pretty. This creates ugly markup, and nearly worthless content. So we are going to now discuss how we can very easily create our own SemanticCheckBoxList and SemanticRadioButtonList controls. They will render their list items to <ul> and <li> tags. This will not only make sense, but it will also allow for easy styling with CSS.

Let's jump right into the code. I'll explain what we are doing through C# comments.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

// All the functionality we want, and almost all of the rendering

// is already found inside of the CheckBoxList class. You can swap

// this out for the RadioButtonList class with no problems.

public class SemanticCheckBoxList : CheckBoxList
{
   // We are going to override one method to get this to work.

}

So far this is very easy. We just need a few namespaces to get our classes from; the System.Web.UI namespace to get the HtmlTextWriter class from, and the System.Web.UI.WebControls namespace to get our CheckBoxList or RadioButtonList class to inherit from. We'll now look at the one simple method we need to override.

protected override void Render(HtmlTextWriter writer)
{
   // We start our un-ordered list tag.

   writer.WriteBeginTag("ul");

   // If the CssClass property has been assigned, we will add

   // the attribute here in our <ul> tag.

   if (this.CssClass.Length > 0)
   {
       writer.WriteAttribute("class", this.CssClass);
   }

   // We need to close off the <ul> tag, but we are not ready

   // to add the closing </ul> tag yet.

   writer.Write(">");

   // Now we will render each child item, which in this case

   // would be our checkboxes.

   for (int i = 0; i < this.Items.Count; i++)
   {
       // We start the <li> (list item) tag.

       writer.WriteFullBeginTag("li");

       this.RenderItem(ListItemType.Item, i, new RepeatInfo(), writer);

       // Close the list item tag. Some people think this is not

       // necessary, but it is for both XHTML and semantic reasons.

       writer.WriteEndTag("li");
   }

   // We finish off by closing our un-ordered list tag.

   writer.WriteEndTag("ul");
}

This took very little code, but the results are very nice. We now have a semantic CheckBoxList and RadioButtonList class. Nothing has changed except for how it renders. Here is the ASP.NET code that you would use to create our new control.

<SingingEels:SemanticCheckBoxList ID="ExampleCheckBoxList" runat="server">
   <asp:ListItem Value="One" Text="One" />
   <asp:ListItem Value="Two" Text="Two" />
   <asp:ListItem Value="Three" Text="Three" />
</SingingEels:SemanticCheckBoxList>

Soon I'll show you how you can make your semantic controls using ASP.NET Control Adapters instead of inheriting from the control and changing it right in the Render method.

  • Oct 14 2007 - 12:55:46 PM Dileno

    It's not 100% clear how you really implement this custom CheckBoxList. What you can do is to add a namespace to the cs file, a namespace that contains the class, like this:

    namespace Semantics

    {

    public class CustomCheckBoxList : CheckBoxList

    {

    // code goes here

    }

    }

    In order to use the custom CheckBoxList, you have to add this line in the top:

    <%@ Register TagPrefix="SingingEels" Namespace="Semantics" %>

    Then you can use this code where you want the CheckBoxList:

    <SingingEels:CustomCheckBoxList ID="ExampleCheckBoxList" runat="server">

    </SingingEels:CustomCheckBoxList>

    Thanks for this article! (And by the way, you really should make it possible to comment on your blog without being a member!)

  • Oct 14 2007 - 01:04:58 PM Dileno

    And I'm sorry I didn't see your other article on Control Adapters (http://www.singingeels.com/Articles/How_To_Control_Adapters.aspx).

    Thanks!

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:42:19 AM - (0.1250016)