[英]Validating a Drop Down List in asp.net using c#?
I have a drop down list on a page, by default the option that is displayed is 'Please Select One'.At the moment users can select that option and gain access to the next page, what I want to do is if 'Please Select One' is selected ensure that access to the next page/step will not be given until an actual option on the drop down list is selected. 我在页面上有一个下拉列表,默认情况下显示的选项是“请选择一个”。此刻,用户可以选择该选项并访问下一页,我想做的是“请选择一个”。选择“一个”,确保在选择下拉列表中的实际选项之前,不会访问下一页/步骤。
Im guessing some sort of If statement but im unsure of how to do this. 我正在猜测某种If语句,但不确定如何执行此操作。
Any help would be great. 任何帮助都会很棒。
This is my code for my ddl 这是我的DDL代码
<td class="question">
Out of Hours Working:
</td>
<td>
<asp:DropDownList ID="ddlout" runat="server" Width="150px">
<asp:ListItem Text="Please Select One"></asp:ListItem>
<asp:ListItem Text="Yes"></asp:ListItem>
<asp:ListItem Text="No"></asp:ListItem>
</asp:DropDownList>
<span class="mandatory">*</span>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator14" runat="server" ControlToValidate="ddlout"
ErrorMessage=" Required." InitialValue="Please select one..."
ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
Instead of using the <asp:RequiredFieldValidator>
use the <asp:CompareValidator>
... 不用
<asp:RequiredFieldValidator>
而是使用<asp:CompareValidator>
...
<asp:CompareValidator
ID="val14" runat="server" ControlToValidate="ddlout"
ErrorMessage=" Required." Operator="NotEqual"
ValueToCompare="Please Select One"
ForeColor="Red" SetFocusOnError="true" />
Note the additional Operator
and ValueToCompare
. 注意附加的
Operator
和ValueToCompare
。 If the value of the dropdown is "not equal" to the "value to compare" then it is ok - otherwise it will fire. 如果下拉菜单的值与“要比较的值”“不相等”,则表示可以,否则将触发。
See MSDN for more information 有关更多信息,请参见MSDN。
I would, however, recommend that you give actual Value
's to each of the ListItem
objects, rather than using the Text alone. 但是,我建议您为每个
ListItem
对象提供实际的Value
,而不要单独使用Text。 For instance <asp:ListItem value="0" Text="Please Select One"/>
which you can then test ValueToCompare="0"
例如
<asp:ListItem value="0" Text="Please Select One"/>
,然后可以测试ValueToCompare="0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.