In most of the examples that I saw on the Internet, there is a fragment in which they use 2 flags, one for one flag (all), and the second - checkboxlist. In my case, I have only one checkboxlist associated with the data source, for example, my data source list options (all, apple, orange, red, blue), I got most of the work, except when everything is not checked, and I check the last element, for example blue, it checks the ALL option. therefore does not work properly. id is the identifier of the Everyone 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>
source
share