Last post Dec 16, 2005 09:30 PM by joteke
Dec 16, 2005 03:44 PM|g5temp|LINK
We have an ASP.NET 2.0 (C#) application that has a web form with a CheckBoxList control and a CustomValidator control. The CustomValidator control is used to validate that at least one checkbox is checked in the CheckBoxList control.
Here's what we got:
function validateChecked(oSrc, args)
args.IsValid = false;
var formToValidate = document.forms['aspnetForm'];
formToValidate = document.aspnetForm;
var numberOfControls = formToValidate.length;
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
element = formToValidate[controlIndex];
if (element.type == "checkbox")
if (element.checked == true)
args.IsValid = true;
want to validate and the other checkboxes that we don't want to validate.
Since ASP.NET assigns some funky IDs/Names when the CheckBoxList is rendered (e.g.,
<input id="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxListDaily_0" type="checkbox" name="ctl00$ContentPlaceHolderMain$Daily1$CheckBoxListDaily$0" /><label for="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxListDaily_0">Sun</label>),
how can you reference the correct controls by ID/Name?
Is there a better way of doing this?
Dec 16, 2005 09:30 PM|joteke|LINK
you can output the ClientID of the CheckBoxList to your script (with <%=%>) and also the count of items in the CBL (with Items.Count). You are on right track that you can get that done with the IDs (yes, ASP.NET can give you the client-side ID via ClientID
property of the control)
So you know what there are items with IDs
So assuming you'd had CBL for example like this:
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
You could have validation script as follows to use with CustomValidator
if(document.getElementById(cblid + '_' + i).checked)
Note that I output the relevant information from server-side to client-side by placing <%=%>'s to the client-side script. Those are executed when page renders so they won't contradict with your server-side processing
of the control(s) on the page.
And note that you also need to implement the server-side validation as well, though that is easy task.