I have a CheckBoxList that I am trying to validate that at least one of the checkboxes is checked.
Markup:
<asp:CustomValidator ID="RequiredFieldValidator8" ValidationGroup="EditArticle"
runat="server" ErrorMessage="At least one Category is required."
OnServerValidate="topic_ServerValidate" />
<asp:CheckBoxList id="checkboxlistCategories" runat="server"></asp:CheckBoxList>
Code-behind:
protected void topic_ServerValidate(object source, ServerValidateEventArgs args)
{
int i = 0;
foreach (ListItem item in checkboxlistCategories.Items)
{
if (item.Selected == true)
i = i + 1;
}
if (i == 0)
args.IsValid = false;
else
args.IsValid = true;
}
If I add ControlToValidate="checkboxlistCategories" in the CustomValidator control, it blows up! The exception I get is:
System.Web.HttpException: Control 'checkboxlistCategories' referenced by the ControlToValidate property of 'RequiredFieldValidator8'
Am I missing something?