Make two checkbox lists mutually exclusive
Asked Answered
W

2

1

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

Woodwaxen answered 13/10, 2011 at 5:51 Comment(1)
You could check the SelectedIndex property of each list - if the value of list 1 is >= 0, disable list 2, and vice versa. You'd also need to handle the situation where a selection was made in one list, and then the selection was cleared (i.e., you'd have to reenable the disabled list at that point). If I had more time I'd try to put together a code sample. Hopefully this will give you something to work with.Solarism
S
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;
        }
    }
}
Solarism answered 13/10, 2011 at 17:18 Comment(5)
Perfect!! working great :) Thank you for this simple solution. One more query, as of now I have enabled auto post back in both the lists. However whenever I select or unselect any one chekbox in the list, entire page is reloading. I know we can use Update Panel of Ajax. Still like to know do we have any work around in ASP.net.Woodwaxen
@Woodwaxen - You're welcome. I'm not sure what you mean by a work around - if you're referring to the full page posting back, the only "work-around" (and it's not what I would consider to be a work-around) that I'm aware of is via AJAX and an UpdatePanel (through ASP.NET).Solarism
@RaghavMac - And the error is? And your code is? It's probably better to post a question with your code than to simply leave a comment on an old answer to another user regarding their code.Solarism
@Solarism How to do the above for windows phone?Overfill
@RaghavMac - I don't know. Why don't you post your code with a question and tag it wp7? Asking a Windows Phone question on an ASP.NET answer will most likely not get you very far.Solarism
W
0

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

Woodwaxen answered 13/10, 2011 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.