下拉列表中的默认值 [英] default value in dropdownl list

查看:72
本文介绍了下拉列表中的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的asp.net c#项目中有几个下拉列表.

我已经设置了默认值,在下拉列表中说选择一个",并为该下拉列表设置了一个验证器.

我的问题是关于buttonclick事件,如果没有选择任何值,我的验证器将不会启动dropdownl列表,并且默认值选择一个"也不会保存在数据库中.

我无法弄清楚如何防止这种默认值存储在我的DATABSE也火验证时所选择的值是默认值选择之一".

在此先感谢

i have several dropdown lists in my asp.net c# project.

i have set default value say "choose one" in drop down list and aslo set a validator for that dropdownlist.

my problem is on buttonclick event my validator does not fire for the dropdownl list and also the default value "choose one" saved in my database if i dont select any value.

i cant figure out how to prevent this default value to store in my databse and also fire validator when the value selected is default value "choose one".

thanks in advance

推荐答案

< asp:DropDownList ID ="inquiry_type" runat ="server" CausesValidation ="True" ValidationGroup ="g1" Width ="180" TabIndex ="1">

< asp:ListItem Value ="0">-选择一个-</asp:ListItem>
< asp:ListItem值="1">选项1</asp:ListItem>
< asp:ListItem Value ="2">选项2</asp:ListItem>
</asp:DropDownList>

< asp:RequiredFieldValidator ID ="RequiredFieldValidator2" runat ="server" ControlToValidate ="inquiry_type" ErrorMessage =必需的查询类型" ValidationGroup ="g1" Style ="display:none" InitialValue ="0">
</asp:RequiredFieldValidator>


将值字段添加到每个列表项.并在所需的字段验证器中将初始值"设置为0.这将帮助您验证下拉菜单,并且选择一个..."将不会发送到DB.


希望这对您有帮助!
<asp:DropDownList ID="inquiry_type" runat="server" CausesValidation="True" ValidationGroup="g1" Width="180" TabIndex="1">

<asp:ListItem Value="0">--Choose one--</asp:ListItem>
<asp:ListItem Value="1">Option 1</asp:ListItem>
<asp:ListItem Value="2">Option 2</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="inquiry_type"ErrorMessage="Inquiry type required" ValidationGroup="g1" Style="display:none" InitialValue="0">
</asp:RequiredFieldValidator>


Add a value field to each list item. And in the required fiels validator set the Initial Value to 0. This will help you to validate your dropdown and the "Choose one..." will not be sent to DB.


Hope this will help you!!!


设置dropdownlist的数据源,然后在下面编写代码

set the datasource of dropdownlist after that write a code below

dropDownList.Items.Insert(0, new ListItem("--choose one--", "0"));



例子



example

dropDownList.DataSource = List.ToString();
dropDownList.DataBind();

dropDownList.Items.Insert(0, new ListItem("-- choose one--", "0"));


如果它是RequiredFieldValidator,则将其InitialValue属性设置为选择一个".这样,如果在下拉菜单中未选择任何内容,验证器将显示错误.如果您使用其他验证程序,请告诉我们.
If it is a RequiredFieldValidator, set it''s InitialValue property to "choose one". This way, validator will show error if nothing is selected in the dropdown. If you are using any other validator, let us know.


这篇关于下拉列表中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆