vb.net 使用asp.net Web表单将值设置为空时验证下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21620315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate dropdown when value is set to empty using asp.net web form
提问by Vignesh Marteen
<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>
I do want to validate dropdown, if selectis selected error should be thrown. Main aim is that the value should be empty.
我确实想验证下拉列表,如果select被选中,应该抛出错误。主要目标是该值应该为空。
Is there is any methord to validate like this . Please help me with this
是否有任何方法可以像这样验证。请在这件事上给予我帮助
回答by Amarnath Balasubramanian
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>
The Important property to be noted in the code is the following
代码中需要注意的重要属性如下
ControlToValidate="ddl"
InitialValue="-Select-"
ValidationGroup="CreateRolls"
回答by David López
Edited answer's Amarnath Balasubramanian (I have not permissions to add comments)
编辑答案的 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>
The Important property to be noted in the code is the following
代码中需要注意的重要属性如下
ControlToValidate="ddl"
InitialValue="-Select-"
ValidationGroup="CreateRolls"
Note that changes are
请注意,更改是
<asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
InitialValue="-1"
回答by Nimmi
<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>
or you can use Required field validator
或者您可以使用必填字段验证器

