Reputation: 498
I have a checkbox that when a checked checks all items in a CheckedListBox
.
When the checkbox goes unchecked it should uncheck all items in the list.
Code:
if (checkBoxCheckAllPrivileges.Checked)
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
for (int i = 0; i < listBoxUsers.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, false);
Is the problem in this code?
Does the .SetitemChecked work giving it the parameter as false?
Is there any other way to uncheck the items?
Upvotes: 4
Views: 3936
Reputation: 1
Use JavaScript Too
$(document).ready(function () {
$("#<%=checkBoxCheckAllPrivileges.ClientID%>").click(function () {
if ($(this).is(":checked"))
{
$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", true);
}
else
{
$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", false);
}
});
Upvotes: -1
Reputation: 911
You have given wrong item in else part for loop,
if (checkBoxCheckAllPrivileges.Checked)
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, false);
Upvotes: 4