Make two checkbox lists mutually exclusive

依然范特西╮ 提交于 2019-12-13 02:19:11

问题


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...


回答1:


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



回答2:


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...



来源:https://stackoverflow.com/questions/7749960/make-two-checkbox-lists-mutually-exclusive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!