开发者

Make two checkbox lists mutually exclusive

开发者 https://www.devze.com 2023-04-13 04:11 出处:网络
Good day friends, I got to know how we can make two checkboxes or checkboxes inside a checkbox list mutually exclusive.

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_selectColumn开发者_开发问答s.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...


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;
        }
    }
}


Hi after working on this issue, with the help of Tim's tips, Finally got it worked. Please provide solutions, if you have any easy one .

protected void CheckBox_selectColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool Is_select = false;
            foreach (ListItem listItem in CheckBox_selectColumns.Items) 
            {
                if (listItem.Selected)
                {
                    Is_select = true;
                }

            }

            if (Is_select)
            {
                foreach (ListItem listItem in CheckBox_SelectAll.Items) 
                {
                   if (listItem.Selected)
                   {
                      listItem.slected=false;
                   }

                }
            }

        }

For the second Checkbox List did the opposite..

        protected void CheckBox_SelectAll_CheckedChanged(object sender, EventArgs e)
        {
           bool Is_select = false;
            foreach (ListItem listItem in CheckBox_SelectAll.Items) 
            {
                if (listItem.Selected)
                {
                    Is_select = true;
                }

            }

            if (Is_select)
            {
                foreach (ListItem listItem in CheckBox_selectColumns.Items) 
                {
                   if (listItem.Selected)
                   {
                      listItem.slected=false;
                   }

                }
            }
        }

This one is working correctly, any suggestions to make the code bit more refined will be really helpful...

0

精彩评论

暂无评论...
验证码 换一张
取 消