My contemplations and diurnal novelties are publicized here

Archive for April 6, 2012

How to get checkboxlist (Asp.net) values using jquery


In asp.net usually checkboxlist is binded with a data source and you need to check the checkboxes value sometime on client side.

How I approached this, attaching a class to list item for Checkboxlist using following code:-

foreach (ListItem li in checkBoxList.Items)        {

li.Attributes[“class”] = “checkBoxListItems”;

}

A checkboxlist in asp.net is usually rendered as

<span>

<input id=”ctl00_ContentPlaceHolder1_ checkBoxListItems_1″ type=”checkbox” name=”ctl00$ContentPlaceHolder1$checkBoxListItems$3″>

<label for=”ctl00_ContentPlaceHolder1_checkBoxListItems _3″>You label here</label>

</span>

So I used jquery to iterate through all the listitem and get the checked items using.

$(‘.btnCompareSelected’).click(function() {
var selectedCount = 0;
$(“. checkBoxListItems “).each(
function(intIndex) {
if ($(this).find(‘input’).attr(‘checked’) == true)
selectedCount++;
});
if (selectedCount < 2) {
alert(‘Atleast select 2 products to compare’);
return false;
}
});

%d bloggers like this: