NovelProjects

Wednesday, January 21, 2009

ASP.NET CheckBoxList

Please, somebody help me figure this one out. All I wanted to do was create a simple CheckBoxList that's built dynamically based off of some database values. I wanted something that goes a little something like this:



<asp:CheckBoxList id="CheckBoxes" runat="server" />


foreach(DataRow row in table.Rows)
{
ListItem item = new ListItem();
item.Name = row["Name"].ToString();
item.Value = row["Value"].ToString();
item.Selected = true;

CheckBoxes.Items.Add(item);
}


Here's the rendered HTML:



<input id="ctl00_PageContent_CheckBoxes_0" name="ctl00$PageContent$CheckBoxes$0" type="checkbox">
<label for="ctl00_PageContent_CheckBoxes_0">##Name##</label>


Guess what: it doesn't work. Well, I should say that it only works server-side. But, if you want to do anything client-side (say, jQuery), there's no value associated with the check box. Really? Am I missing something here? Let's look at the HTML documentation for a check box:



HTML Forms and Input



<input type="checkbox" name="vehicle" value="Bike">


Uh oh, what's that? A value property?? Clearly I'm not dreaming that I want a value associated with a check box. Apparently, .NET strips out any value parameters from it's check boxes and will only work when dealing with post-backs (server-side). On top of that, I tried to insert my own attribute on those dynamically built check boxes, but got the same result. Am I missing something here? Doesn't it make sense to write correct HTML with the value attribute?

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.