Asp.net
asp.net 中的下拉列表值
我想向下拉列表中添加一個無法選擇的值,例如標題。例如:我有一個月的下拉列表。第一項應該是“選擇月份”,這不應該被選中。接下來是從一月到十二月。我怎樣才能做到這一點?
這是我目前的程式碼。
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>你甚至可以使用
RequiredFieldValidatorwhich ignore this item,它認為它是未選中的。<asp:RequiredFieldValidator ID="ReqMonth" runat="server" ControlToValidate="DdlMonths" InitialValue="-1"> </asp:RequiredFieldValidator>