I have a listbox like so:
<asp:ListBox runat="server" ID="lbSelectedUsers" SelectionMode="Multiple" Height="200" Width="100"></asp:ListBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ValidationGroup="ValidationGroup" ErrorMessage="No users selected." ControlToValidate="lbSelectedUsers"</asp:RequiredFieldValidator>
The listbox is initially empty but then gets populated based on users selection. The issue I have is that after items get added the validation still fails.
It only works if a user actually clicks(selects) an item on the list.
I want to the validation to fail only when the listbox is empty. I know I can make my custom workaround but I want this to work with a validation summary which checks other controls on the page.
Can that be done?
That is not really what the RequiredFieldValidator is for - it only checks if something has been selected or not.
In your case you need to use a Custom Validator (http://msdn.microsoft.com/en-us/library/9eee01cx(VS.80).aspx) - this will work with the ValidationSummary control the same as any other Validation control (as long as you correctly set the ValidationGroup if being used).
All you then need to do is write a custom "ClientValidationFunction" which will return false if no items or true if it has items.