Unchecking two items in a checkboxlist?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I need to tell a checkbox list that if two of the items have been checked,
to uncheck themselves. I need help! Thanks :)

~Chelimar
 
It sounds like you may want to switch to a RadioButtonList, then.
That's the default behavior of a radio button group (selecting one
deselects the previous).

Two benefits:
1. Less code for you to write, debug, and maintain
2. Standard HTML form behavior so your users will not be confused. A
checkbox list _should_ allow multiple selections according to the HTML
specs.

If you're stuck with this due to business requirements (I've been
there, too), you'll have to do this with Javascript on the client.
Here's some sample code to get you started (needs to be converted to a
webform):

<html>
<head>
<script>
function VerifyCheckboxes()
{
count = document.frm1.elements.length;
checkedElements = 0;
for (i=0; i < count; i++)
{
if(document.frm1.elements.checked == 1)
{
checkedElements++;
if(checkedElements>1)
{
uncheckAll();
}
}
}
}

function uncheckAll()
{
for (i=0; i < count; i++)
{
document.frm1.elements.checked = 0;
}
}
</script>
</head>
<body>
<form name="frm1">Sample
<input type="checkbox" onclick="VerifyCheckboxes()" value="1"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="2"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="3"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="4"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="5"/>
</form>
</body>
</html>
 
Write one method which does this work and attach it to onclicked
eventhandler of both the checkbox.
 
Back
Top