CheckedlistBox
  • I have a form that contains two elements: a CheckedListBox and a CheckBox. The CheckBox, which is called SelectAllCheckBox, is used to check/uncheck all items in the CheckedListBox. I achieve this via a CheckedChanged event handler associated with the SelectAllCheckBox, so that when it is checked, all items in the CheckedListBox are checked, and vice versa. This works fine.

    I also have code that unchecks the SelectAllCheckBox when the user unchecks one of the CheckBoxes in the CheckedListBox. For example, if the user checks the SelectAllCheckBox, and then unchecks one of the items, the Select All CheckBox should be unchecked. This is achieved via the CheckedListBox.ItemChecked event handler. This also works fine.

    My problem is that when the SelectAllCheckBox is programatically unchecked (as in the above scenario), its event handler causes all items in the CheckedListBox to become unchecked.

    I'm sure others have run into my issue; is there an elegant workaround?
  • 4 Comments sorted by
  •    private void AllStaffCheckBox_Click(object sender, EventArgs e)
            {
                if (AllStaffCheckBox.Checked)
                    for (int i = 0; i < StaffListBox.Items.Count; i++)
                    {
                        StaffListBox.SetItemChecked(i, true);
                    }
                else
                    for (int i = 0; i < StaffListBox.Items.Count; i++)
                    {
                        StaffListBox.SetItemChecked(i, false);
                    }
            }
       private void StaffListBox_ItemCheck(object sender, ItemCheckEventArgs e)
            {
                if (e.NewValue == CheckState.Unchecked)
                {
                    if (StaffListBox.CheckedItems.Count == StaffListBox.Items.Count)
                    {
                        AllStaffCheckBox.Checked = false;
                    }
                }
                else if (e.NewValue == CheckState.Checked)
                { 
                if (StaffListBox.CheckedItems.Count == StaffListBox.Items.Count-1)
                    {
                        AllStaffCheckBox.Checked = true;
                    }
                }
            }
  • private void AllStaffCheckBox_Click(object sender, EventArgs e)
    {
    if (AllStaffCheckBox.Checked)
    for (int i = 0; i < StaffListBox.Items.Count; i++)
    {
    StaffListBox.SetItemChecked(i, true);
    }
    else
    for (int i = 0; i < StaffListBox.Items.Count; i++)
    {
    StaffListBox.SetItemChecked(i, false);
    }
    }
    private void StaffListBox_ItemCheck(object sender, ItemCheckEventArgs e)
    {
    if (e.NewValue == CheckState.Unchecked)
    {
    if (StaffListBox.CheckedItems.Count == StaffListBox.Items.Count)
    {
    AllStaffCheckBox.Checked = false;
    }
    }
    else if (e.NewValue == CheckState.Checked)
    {
    if (StaffListBox.CheckedItems.Count == StaffListBox.Items.Count+1)
    {
    AllStaffCheckBox.Checked = true;
    }
    }
    }
  •    <div>
            <asp:CheckBox ID="CheckBox1" runat="server" Text="Select All" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True"/>
             <br />
            <br />
            <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" OnTextChanged="CheckBoxList1_TextChanged" >
                <asp:ListItem Text="One" Value="1"></asp:ListItem>
                <asp:ListItem Text="Two" Value="2"></asp:ListItem>
                <asp:ListItem Text="Three" Value="3"></asp:ListItem>
            </asp:CheckBoxList>
        </div>
    ________________________________________

     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int check = 0;
            foreach (ListItem li in CheckBoxList1.Items)
            {

                if (li.Selected)
                {             

                }
                else {
                    check = 1;
                }           
            }
            if (check == 0)
            {
                CheckBox1.Checked = true;
            }
            else if (check == 1)
            {
                CheckBox1.Checked = false;
            }
        }
        protected void CheckBoxList1_TextChanged(object sender, EventArgs e)
        {



        }
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked)
            {
                foreach (ListItem li in CheckBoxList1.Items)
                {
                    li.Selected = true;

                }
            }
            else {
                foreach (ListItem li in CheckBoxList1.Items)
                {
                    li.Selected = false;

                }
            }

        }



  • I hope my code is very easy and running successfully Mr.Aschary

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

    Optimum Creative