I’m working with an asp.net 4.0 application and I am wondering if its possible to set the class attribute on the checkbox (the input tag) created by a checkbox list control.
What the following does is wrap the input tag in a span tag and apply the class to that:
foreach (ListItem li in CheckBoxList1.Items)
{
li.Attributes.Add("class", "check-if-dirty");
}
Creates:
<span class="check-if-dirty"><input id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="a" /><label for="MainContent_CheckBoxList1_0">a</label></span>
What I want is this:
<span><input class="check-if-dirty" id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="a" /><label for="MainContent_CheckBoxList1_0">a</label></span>
Thank you.
I know jQuery isn’t tagged on here, but here’s some simple code that will accomplish that, if using this library happens to be an option for you.
There might be a way to get asp.net asp.net to do what you want by messing around in the PreRender part of the page lifecycle. If this answer doesn’t work for you, you might look into that. Sorry I can’t be a better help.