asp:CustomValidator / OnServerValidate
Posted
by Neil
on Stack Overflow
See other posts from Stack Overflow
or by Neil
Published on 2010-03-23T14:24:37Z
Indexed on
2010/03/23
14:33 UTC
Read the original article
Hit count: 490
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! Am I missing something?
Thanks in advance.
© Stack Overflow or respective owner