most examples i have seen online have a snippet where they use 2 checkboxes, one for just one checkbox (all) and the second is the checkboxlist. In myy case i have only one checkboxlist bound to datasource, example my datasource list options are(All,apple,orange,red,blue), I got most of it working except when everything is unchecked and i check the last item, example blue, it checks the ALL option. so not working properly. id is the id of the ‘All’ item in the list
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var id = "#<%=cbOptions.ClientID %>_0";
var checkboxlistid = "#<%= cbOptions.ClientID %>";
$(id).click(function () {
$("#<%= cbOptions.ClientID %> input:checkbox").attr('checked', this.checked);
});
$(checkboxlistid + " input:checkbox").click(function () {
if ($(checkboxlistid).attr('value') != 0) {
if ($(id).attr('checked') == true && this.checked == false) {
$(id).attr('checked', false);
}
else {
if ($(id).attr('checked') == true && this.checked == true)
CheckSelectAll();
}
}
});
function CheckSelectAll() {
var flag = true;
$(checkboxlistid + " input:checkbox").each(function () {
if ($(checkboxlistid).attr('value') != 0) {
if (this.checked == false) {
flag = false;
}
else {
if ($(id).attr('checked') == true && this.checked == false) {
flag = false;
}
else {
flag = true;
}
}
}
});
$(id).attr('checked', flag);
}
});
</script>
<asp:CheckBoxList runat="server" ID="cbOptions" >
</asp:CheckBoxList>
</asp:Content>
1 Answer