Check all checkboxes in checkboxlist with one click using c#

*爱你&永不变心* 提交于 2019-12-02 23:54:27
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
}

Call a method from code behind in C# and write this piece of code, then you could be able to check/uncheck them. This checks or uncheck all the check boxes present in the checkboxlist. Hope it might help.

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;    
}

Try this...

    protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox[] boxes = new CheckBox[7];
        boxes[0] = this.CheckBoxID;
        boxes[1] = this.CheckBoxID;
        boxes[2] = this.CheckBoxID;
        boxes[3] = this.CheckBoxID;
        boxes[4] = this.CheckBoxID;
        boxes[5] = this.CheckBoxID;
        boxes[6] = this.CheckBoxID; //you can add checkboxes as you want

        CheckBox chkBox = (CheckBox)sender;
        string chkID = chkBox.ID;
        bool allChecked = true;

        if (chkBox.Checked == false)
            allChecked = false;

        foreach (CheckBox chkBoxes in boxes)
        {
            if (chkBox.Checked == true)
            {
                if (chkBoxes.Checked == false)
                    allChecked = false;
            }
        }
        this.CheckBoxIDALL.Checked = allChecked; //Here place the main CheckBox
    }

Try this:

 foreach(Control c in this.Controls) {
    if (c.GetType() == typeof(CheckBox)) {
       ((CheckBox)c).Checked = true;
    }
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!