1

A Bug in ASP.NET CheckboxList

by volkanuzun 23. July 2009 22:46

I work for a state university; which requires our websites to be compliant with Section 508 guidelines. It is not that much of a work you have to do; if you follow a few things while you are coding. One of the requirements of this guideline is; if you have checkboxes and no text next to them (think like a table where in the column header it says:”Click if you will attend”; and checkboxes in each row); then these checkboxes should have title attribute with a description value. The reason behind this requirement is; if a blind user is visiting your page; they wont see the table layout; and relate the checkbox with the column header. So in this application that I was working on; I had asp.net checkboxlist control on the page; and in the codebehind file I was creating the ListItems dynamically and adding them to checkblostlist as below:

ListItem li = new ListItem(String.Empty, program.ID.ToString());
li.Attributes.Add("title",program.Name);
listProgramCoordinator.Items.Add(li);

It is a simple code that just inside a loop; ListItems are created; adding the title attribute to them; and then they are being added to CheckBoxList. I was so sure that the above code will generate “<input type=”checkbox” title=”… I didn't even test it.

A few days after the code was published; our compliance officer called me; and reminded me that I needed to put the title attribute to the checkboxes !!!. I opened the website in my browser; and checked page source view; and was shocked. The asp.net framework was wrapping the checkbox with a span block; putting the title to the span block; and not putting any titles to checkboxes; so what i was getting: “<span title=”some title”><input type=”checkbox”…. I could get around this; so I decided to send an email to Scott Hanselman who is really a great guy and helpful person. He  kindly forwarded my email to some other people at Microsoft probably working at the ASP.NET team. Scott Galloway contacted me; and told me that this is a bug with checkboxlist which is an old control hence does not follow the pattern of adding controls for each row. After Scott Galloway; Eilon Lipton sent an email too; this time he gave some explanation why this control does not work. Here is his explanation:

"

The CheckBox has two collections of attributes:

1. The “Attributes” collection that gets applied to the <span>

2. The “InputAttributes” collection that gets applied to the <input>

Unfortunately the CheckBoxList only allows you to modify Attributes and not InputAttributes.

So to cut the long story short; both advised me to use a repeater to generate checkboxes; which I could do; but I also asked help from my friend James Johnson before I contacted Scott Hanselman. He helped me and coded  a simple jquery function that iterates through the checkboxes; getting the title from the wrapping span tag; removing it from there to checkboxes.

I thank everybody who helped me.

Tags:

Comments

volkanuzun
volkanuzun United States
9/25/2009 7:45:08 AM #

here is the query:
<script type="text/javascript">
        $(document).ready(function() {
            AddAttributes();
        });

        function AddAttributes() {
            $("#listProgramCoordinator").find("input[type=checkbox]").each(function(i) {
                var parentSpan = $(this).parent("span");
                var title = $(parentSpan).attr("title");
                $(this).attr("title", title);
                $(this).next("label").attr("title", title);
                $(parentSpan).removeAttr("title");
            });
        }
    </script>

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Original Design by Laptop Geek, Adapted by onesoft