Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox Groupname? 2

Status
Not open for further replies.

romh

Programmer
Jan 3, 2003
297
US
Hi. Is there a way to make a checkbox behave the same way that you would create a radiobutton group? I need a series of checkboxes in which only 1 can be selected.
1 idea that comes to mind is to use the checkboxes

OnCheckedChanged

to clear any other checkboxes that could be checked.
Is that the only way?

Thanks

 
You could do that, but why? That's why radio buttons and radio button lists are available.

Jim
 
first of all, thanks for your help. The reason is that the requirements are for them to be checkboxes. I agree with you though. Radio is much much more suited.


 
You may want to explain to them what the difference is between the 2. If they still insist.. then your solutions is the only way I know of to do what you want to do.

Jim
 
I agree that the difference is purely aesthetic, or so it seems. One way to pull this off in javascript is as follows (fyi):
Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<html>
<head>
<title>CheckBox Test</title>
</head>
<script type="text/javascript">
function checkAll(el) {
  var form = el.form;
  var els = form.elements;
  for (var x = 0; x < els.length; x++) {
    if (el != els[x]
      && els[x].type
      && els[x].type.toLowerCase() == "checkbox") {
      els[x].checked = false;
      if (els[x].name==el.name){
        els[x].checked = true;
      }
    }
  }
}
</script>
<body>
<form id="Form1" runat="server">
  <asp:checkbox id="A" runat="server" onClick="checkAll(this);"/> 
  <asp:checkbox id="B" runat="server" onClick="checkAll(this);"/> 
  <asp:checkbox id="C" runat="server" onClick="checkAll(this);"/> 
  <asp:checkbox id="D" runat="server" onClick="checkAll(this);"/> 
  <asp:checkbox id="E" runat="server" onClick="checkAll(this);"/> 
  <asp:checkbox id="F" runat="server" onClick="checkAll(this);"/> 
</form>
</body>
</html>
..but I would agree with ca8msm and Jim, that it would probably be more efficient to use radiobuttons.
 
One problem with the above Javascript was that it did not allow for the "deselection" of a checkbox; which is probably needed. The following Javascrip allows for the selection and deselection of any checkbox:
Code:
<script type="text/javascript">
function checkAll(el){
  var form = el.form;
  var els = form.elements;
  for (var x = 0; x < els.length; x++){
    if (el != els[x]
      && els[x].type
      && els[x].type.toLowerCase() == "checkbox"
      && els[x].name != el.name){
      els[x].checked = false;
      }
    }
  }
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top