Remove the special validator, add the desired error message to it, double-click on the custom validator to get the code behind the event handler, and then you will implement the server command as follows:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = ListBox1.Items.Count > 0;
}
You can also implement client-side javascript.
I just threw it on the page and quickly tested it, so you may need to tweak it a bit: (Button1 adds an item to the list box)
<script language="JavaScript">
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="NOVALID" />
<asp:Button ID="Button2" runat="server" Text="ButtonsUBMIT" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate" ClientValidationFunction="ListBoxValid"></asp:CustomValidator>
If you add a check summary to the page, the error text should appear in this summary if there are no items in the ListBox or other control that you want to use, unless the ValidationGroup is the same.
source
share