列表框验证

时间:2010-08-22 17:06:08

标签: asp.net

我想验证listBox是否列表框是空的我正在做以下

 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> 

但它不适用于空列表框  如何检查?请让我知道

1 个答案:

答案 0 :(得分:0)

更新:回答重写。

原始提问者找到了以下答案(请参阅评论):您必须将ValidateEmptyText设置为true。