list box validation
i want to validate for listBox wheth开发者_JAVA技巧er list box is empty of or not i am doing following
protected void isToListEmpty_ServerValidate(object source, ServerValidateEventArgs args)
{
if (toListBox.Items.Count == 0)
{
args.IsValid = false;
}
else args.IsValid = true;
}
<td>
<asp:UpdatePanel ID="second" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListBox id="toListBox" runat="server" Width="150px" Height="200" SelectionMode="Multiple">
</asp:ListBox>
<asp:CustomValidator id="isToListEmpty" runat="server" OnServerValidate="isToListEmpty_ServerValidate" ErrorMessage="Select Student Details" ValidationGroup="verify" ControlToValidate="toListBox"></asp:CustomValidator>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="ButtonRemove" EventName="Click"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="ButtonAddAll" EventName="Click"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="ButtonRemoveAll" EventName="Click"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
</td>
but it doesn't work for empty list box How to check for this? please let me know
Update: answer rewritten.
The original asker found the following answer (see comments): you must set ValidateEmptyText
to true.
精彩评论