我有一个网页表单,其中有三个下拉菜单,如下图所示:
上图中的表单有3个下拉列表(负责操作的物理人员,Resident,Staff ),我的任务是如果没有选择任何内容,则应显示警告消息{{1 }}
我为实现这个目的而使用的.aspx代码是:
Please Select an Instrument Type
上述代码似乎无法验证,因为如果我没有从下拉列表中选择任何内容,它就不会显示任何警告消息。我想知道我在做什么错误以及我需要在上面的代码中做些什么更改,以便在我不选择任何下拉列表时成功显示警告。
答案 0 :(得分:1)
我希望您的DropDownList
在ListItem
属性中没有定义InitialValue
项值,因此您需要使用其他值来触发验证:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" InitialValue="0" ControlToValidate="ddlInstrumentType2"
ErrorMessage="Please select an Instrument Type" ...>*
</asp:RequiredFieldValidator>
注意:确保RequiredFieldValidator
中定义的验证组名称存在。
或者在DropDownList
(应该绑定数据源)中,使用带有初始值的ListItem
作为第一项:
<asp:DropDownList ID="ddlInstrumentType2" runat="server" AutoPostBack="true">
<asp:ListItem Text="--------- Select Instrument Type ---------" Value="--------- Select Instrument Type ---------"></asp:ListItem>
</asp:DropDownList>
由于RequiredFieldValidator
控件仅在ListItem
为空时有效,因此第一种方法应该足够了。