you can just use a RequiredFieldValidator, for example <asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple"> <asp:ListItem Value="">Please Select At Least One</asp:ListItem> <asp:ListItem Value=".89">apples</asp:ListItem> <asp:ListItem Value=".49">bananas</asp:ListItem> <asp:ListItem Value="2.99">cherries</asp:ListItem> <asp:ListItem Value="1.49">grapes</asp:ListItem> <asp:ListItem Value="2.00">mangos</asp:ListItem> <asp:ListItem Value="1.09">oranges</asp:ListItem> </asp:ListBox>
>>>要验证的是在提交时这里面至少已添加了一个listitemyou only need to remember the old count of options for the SELECT and check the new count of options to see if the latter is larger>>dropdownlist的listitem都是动态添加的?if you mean they are added by javascript, then I can warn you, it will probably not work, since DropDownList uses ViewState to restore its ListItemCollection, consider to add the new item(s) to a hidden variable...
like I said, if you only need to 验证在第一个listbox里到底有没有选项, you could just use a RequireFieldValidator, if you insist on using CustomValidator, as you might know, CustomValidator is not triggered if the value of the validating ListBox is empty (that means you still need a RequireFieldValidator)of course, you can always write an onsubmit event handler for your form and validate anything you want
RequiredFieldValidator1.ControlToValiddate = ListBox1;
另外,RequiredFieldValidator1.ControlToValiddate = ListBox1 不行,我试过了
<asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="">Please Select At Least One</asp:ListItem>
<asp:ListItem Value=".89">apples</asp:ListItem>
<asp:ListItem Value=".49">bananas</asp:ListItem>
<asp:ListItem Value="2.99">cherries</asp:ListItem>
<asp:ListItem Value="1.49">grapes</asp:ListItem>
<asp:ListItem Value="2.00">mangos</asp:ListItem>
<asp:ListItem Value="1.09">oranges</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
InitialValue=""
ControlToValidate="ListBox1"
ErrorMessage="Required field!"
runat="server"/>
第二点,选项的添删不是用js完成的,而是回送到服务端完成的,应该不存在你说的第二个问题吧,我只是想验证在第一个listbox里到底有没有选项,同时想让验证出错时显示一条红色的自定义消息(与普通文本框验证一样),有没有好的办法解决啊