Check all checkboxes in checkboxlist with one click using c#

后端 未结 6 965
傲寒
傲寒 2021-02-03 22:46

I want to have a button that once clicked, it will select all checkboxes in my checklistbox. I\'ve search the possible answers but I always see examples for asp.net and javascri

6条回答
  •  我寻月下人不归
    2021-02-03 23:16

    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
        }
    

提交回复
热议问题