<asp:DropDownList runat="server" ID="ddl">
<asp:ListItem Text="-Select-" Value=""></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
<asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="cvddl" runat="server" Text="Error"
ControlToValidate="ddl" Operator="NotEqual" ValueToCompare = ""
ValidationGroup="CreateRolls"></asp:CompareValidator>
我确实要验证下拉列表,如果选择了select
,则应该抛出错误。主要目的是价值应该是空的。
是否有任何方法可以像这样验证。 请帮帮我这个
答案 0 :(得分:4)
<强>的DropDownList 强>
<asp:DropDownList ID="ddl" runat="server"
ValidationGroup="CreateRolls"
AppendDataBoundItems="true">
<asp:ListItem Text="-Select-" Value=""></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
<asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>
RequiredFieldValidator
<asp:RequiredFieldValidator ID="rfvDDL" runat="server"
ControlToValidate="ddl"
Display="Dynamic"
ErrorMessage="Values is required."
InitialValue="-Select-"
ForeColor="Red"
ValidationGroup="CreateRolls" >
</asp:RequiredFieldValidator>
代码中需要注意的重要属性如下
ControlToValidate="ddl"
InitialValue="-Select-"
ValidationGroup="CreateRolls"
答案 1 :(得分:1)
<asp:DropDownList runat="server" ID="ddRoleType">
<asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
<asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidator1" runat="server" Text="Error"
ControlToValidate="ddRoleType" Operator="NotEqual" ValueToCompare = "-1"
ValidationGroup="CreateRolls"></asp:CompareValidator>
或者您可以使用必填字段验证器
答案 2 :(得分:1)
已编辑答案的Amarnath Balasubramanian(我无权添加评论)
DropDownList
<asp:DropDownList ID="ddl" runat="server"
ValidationGroup="CreateRolls"
AppendDataBoundItems="true">
<asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
<asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>
RequiredFieldValidator
<asp:RequiredFieldValidator ID="rfvDDL" runat="server"
ControlToValidate="ddl"
Display="Dynamic"
ErrorMessage="Values is required."
InitialValue="-1"
ForeColor="Red"
ValidationGroup="CreateRolls" >
</asp:RequiredFieldValidator>
代码中要注意的重要属性如下
ControlToValidate="ddl"
InitialValue="-Select-"
ValidationGroup="CreateRolls"
请注意,更改是
<asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
InitialValue="-1"