本文介绍了asp.net 中的下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在无法选择的下拉列表中添加一个值,例如标题.例如:我有一个月的下拉列表.第一个项目应该是选择月份",这不应该被选中.接下来是从一月到十二月.我该怎么做?
I want to add a value to the drop down list that cannot be selected, like a title.Ex: I have a month drop down list. The very first item should be "select month" this should not be selected. And next is from January to december. How can I do that?
这是我当前的代码.
string selectmonth = "select * from tblmonth";
SqlCommand scmselect = new SqlCommand(selectmonth, scnbuboy);
SqlDataReader sdrselect = scmselect.ExecuteReader();
drmonth.DataTextField = "month";
drmonth.DataValueField = "monthID";
drmonth.DataSource = sdrselect;
drmonth.DataBind();
推荐答案
<asp:DropDownList ID="DdlMonths" runat="server">
<asp:ListItem Enabled="true" Text="Select Month" Value="-1"></asp:ListItem>
<asp:ListItem Text="January" Value="1"></asp:ListItem>
<asp:ListItem Text="February" Value="2"></asp:ListItem>
....
<asp:ListItem Text="December" Value="12"></asp:ListItem>
</asp:DropDownList>
你甚至可以使用一个 RequiredFieldValidator
忽略这个项目,它认为它没有被选中.
You can even use a RequiredFieldValidator
which ignore this item, it considers it as unselected.
<asp:RequiredFieldValidator ID="ReqMonth" runat="server" ControlToValidate="DdlMonths"
InitialValue="-1">
</asp:RequiredFieldValidator>
这篇关于asp.net 中的下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!