使两个复选框列表互斥

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

#0

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

#1

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

推荐文章

在Matlab的imresize函数中,用来插值的算法是什么?

在Matlab的imresize函数中,用来插值的算法是什么?

推荐文章

用linq和实体框架更新复杂对象

用linq和实体框架更新复杂对象

推荐文章

wpf datagrid仍然隐藏滚动条

wpf datagrid仍然隐藏滚动条

推荐文章

php创建rss提要(比如投票网站)

php创建rss提要(比如投票网站)

推荐文章

从类库中的文件夹读取文件

从类库中的文件夹读取文件

推荐文章

MediaWiki需要一个页面来显示来自多个模板的信息

MediaWiki需要一个页面来显示来自多个模板的信息

推荐文章

依赖于值的PHP if语句

依赖于值的PHP if语句

推荐文章

收集Linux命令输出

收集Linux命令输出

推荐文章

不带quicktime的iPhone M3U音频流

不带quicktime的iPhone M3U音频流

推荐文章

asp.net c#Regex url解析

asp.net c#Regex url解析

推荐文章

如何遍历这个散列?

如何遍历这个散列?

推荐文章

自定义WPF绑定

自定义WPF绑定

推荐文章

web服务返回时如何避免HTML脚本

web服务返回时如何避免HTML脚本

推荐文章

rownum有问题

rownum有问题

推荐文章

你能用CSS在PNG内容中添加一个非正方形的阴影吗?

你能用CSS在PNG内容中添加一个非正方形的阴影吗?