C# – Make two checkbox lists mutually exclusive

asp.netccheckboxcheckboxlist

Good day friends,

I got to know how we can make two checkboxes or checkboxes inside a checkbox list mutually exclusive.
However my question is little different from that, Hope to get some help from stack overflow,

Well I have two checkbox lists, as follows and from a dataset table I am getting the check box values,

CheckBoxlist1 – Checkbox_selectColumns

 if (IsDataSetNotEmpty(ds))
 {
   CheckBox_selectColumns.Items.Clear();
   foreach (DataRow row in ds.Tables[0].Rows)
   {
     CheckBox_selectColumns.Items.Add(row[0].ToString());
   }

 }

CheckBoxlist2 – Checkbox_selectFields

if (IsDataSetNotEmpty(ds))
{
  Checkbox_selectFields.Items.Clear();
  foreach (DataRow row in ds.Tables[1].Rows)
  {
    CheckBox_selectColumns.Items.Add(row[0].ToString());
  }

}

I will get following checkboxes in each lists.

Checkbox_selectColumns : Employee ID, First Name, Last Name

Checkbox_selectFields : manager ID, Manager FName, Manager LName

Is there any way , I can make these two checkboxes mutually exclusive, That is if I select any one or more checkbox from first list, I should not select any checkboxes from second list and vice versa..

Thank you…

Best Answer

Rather than loop through the items in the CheckBox, I'd suggest using the SelectedValue property of the control, as that persists through postbacks (SelectedIndex does not) (ListControl.SelectedValue Property):

protected void CheckBox_selectColumns_SelectedIndexChanged(object sender, EventArgs e)         
{             

    if (CheckBox_selectColumns.SelectedValue != "")
    {
        foreach (ListItem listItem in CheckBox_SelectAll.Items)
        {
            listItem.Selected = false;
        }
    }
}

protected void CheckBox_SelectAll_CheckChanged(object sender, EventArgs e)
{

    if (CheckBox_SelectAll.SelectedValue != "")
    {
        foreach (ListItem listItem in CheckBox_selectColumns.Items)
        {
            listItem.Selected = false;
        }
    }
}