• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

ASP.Net/C# - Is there a way to make a group of checkboxes mutually exclusive?

Hi all.  This is not an easy one I beleive.  I am creating radiobuttons currently with a groupname all in code dynamically.  Well the user now needs to be able to deselect one.  I know I could create one more control with "none" as an option.  Is there a way though to create checkboxes that will do this?  Through my loop I create controls such as this in a control array and you can see I dynamically create its ID as well:

cbl.ID = "cbl" + count; //i.e. ID = rbl1_23
cbl.Attributes.Add("Price", formattedPriceControl);
cbl.Attributes.Add("ItemID", row["ItemID"].ToString());
myControls.Add(cbl.ID);
lit.Text = sbAddBookItems.ToString();
this.Form1.Controls.Add(lit);
this.Form1.Controls.Add(cbl);
cbl = new CheckBox(); //reset the CheckBoxList to a new one
lit = new Literal();
0
sbornstein2
Asked:
sbornstein2
1 Solution
 
VelioCommented:

to do this client-side, one solution is to add the following script to your page
    <script language="javascript" type="text/javascript">
        function ExclusiveCheck(checkBox)
        {
            for (i = 0; i < myCheckboxes.length; i++)
            {
              if (document.getElementById(myCheckboxes[i]) != checkBox)
                document.getElementById(myCheckboxes[i]).checked = false;
            }
             
        }
    </script>

then add the following lines in the loop when you create your checkboxes:

cbl.Attributes.Add("onclick", "ExclusiveCheck(this);");
Page.RegisterArrayDeclaration("myCheckboxes", String.Format("'{0}'", cbl.ID));


-- OR --
have your checkboxes AutoPostBack = true and do it on the server-side, which is rather undesirable in my opinion.

0
 
divinewind80Commented:
My suggestion would be to use Javascript to accomplish your task.  Is this an option?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now