我想向下拉列表添加一个无法选择的值,例如标题。
例如:我有一个月的下拉列表。第一项应该是“选择月份”,不应选择。接下来是一月到十二月。我怎样才能做到这一点?
这是我当前的代码。
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
忽略此项,它认为它是未选择的。
<asp:RequiredFieldValidator ID="ReqMonth" runat="server" ControlToValidate="DdlMonths"
InitialValue="-1">
</asp:RequiredFieldValidator>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)