2013-08-21 73 views
3

我想添加一個值到下拉列表中,不能選擇,如標題。例如:我有一個月的下拉列表。第一個項目應該是「選擇月份」,這不應該被選中。接下來是從1月到12月。我怎樣才能做到這一點?在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(); 
+1

你試過了嗎? –

回答

7
<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> 
+1

謝謝。終於明白了。我認爲我仍然必須對下拉列表本身做些什麼。 – wormwood

2

您可以添加一個空值,如:

ddlmonths.Items.Insert(0, new ListItem("Select Month", "")) 

而且只需添加一個驗證,以防止艇員選拔空的選項,如asp:RequiredFieldValidator

0

假設你有一個叫下來一滴ddlMonths

ddlMonths.Items.Insert(0,new ListItem("Select a month","-1"); 
+0

我試過了: string selectmonth =「select * from tblmonths」; SqlCommand scmselect = new SqlCommand(selectmonth,scnbuboy); SqlDataReader sdrselect = scmselect.ExecuteReader(); drmonth.DataTextField =「month」; drmonth.DataValueField =「monthID」; (0,new ListItem(「select a month」,「 - 1」)); drmonth.DataSource = sdrselect; drmonth.DataBind(); sdrselect.Close(); 但是隻有來自數據庫的數據出現,而不是「select a month」字段。 – wormwood

0

以簡單的方式,它是不可能的。由於DropdownList包含ListItem,它會默認

進行選擇,但,你可以使用ValidationControl爲:

<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic" 
ValidationGroup="g1" runat="server" ControlToValidate="ControlID" 
Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator> 
0

你可以試試這個

ddlMonths.Items.Insert(0,新的ListItem (「選擇」,「」);

1

這些都是很好的答案,如果你想努力工作,但我的猜測是你已經有了你想要的物品來自databound元素,並且只希望在該列表的頂部添加「嘿,夥計 - 挑選一個!」選項。假設是這樣的...

這是簡單的答案。 它總是工作...

  1. 做你的數據綁定列表就像你的計劃。
  2. 然後,在Visual Studio中,修改的項目上的下拉列表中,
  3. 添加一個說明書項目,讓你的「選擇一個項目」的選擇,
  4. 使用在VS2012的項目屬性窗口中,檢查它作爲選擇。現在關閉那個窗口。
  5. 現在,轉到左下方Visual Studio中的屬性框(確保選中下拉列表),然後查找屬性「AppendDataBoundItems」。
  6. 它會讀取False,將其設置爲True。

現在您將得到一個包含所有數據項的下拉菜單,前面帶有手動項目中的「選擇項目」語句。嘗試給它一個默認值,如果可能的話,這將消除您可能遇到的任何錯誤。缺省值爲零,所以如果零不是問題,那麼將其置於零,如果零是一個問題,請將項目中的默認零置換爲不會使您的代碼崩潰的東西。

並停止工作這麼辛苦......這就是Visual Studio的用途。

0
 Dim ListItem1 As New ListItem() 
     ListItem1.Text = "put anything here" 
     ListItem1.Value = "0" 
     drpTag.DataBind() 
     drpTag.Items.Insert(0, ListItem1) 



<asp:CompareValidator ID="CompareValidator1" runat="server" 
    ErrorMessage="CompareValidator" ControlToValidate="drpTag" 
    ValueToCompare="0"></asp:CompareValidator> 
0

試試這個

<asp:DropDownList ID="ddList" runat="server"> 
    <asp:ListItem Value="">--Select Month--</asp:ListItem> 
    <asp:ListItem Value="1">January</asp:ListItem> 
    <asp:ListItem Value="2">Feburary</asp:ListItem> 
    ... 
    <asp:ListItem Value="12">December</asp:ListItem> 
    </asp:DropDownList> 

值應爲默認選擇列表項是空的,那麼它工作正常

0

添加項目使用C#在asp.net與價值的下拉列表只需添加以下代碼例如:

 try 
     { 
      SqlConnection conn = new SqlConnection(conStr); 
      SqlCommand comm = conn.CreateCommand(); 
      comm = conn.CreateCommand(); 
      comm.CommandText = "SELECT title,gid from Groups"; 
      comm.CommandType = CommandType.Text; 
      conn.Open(); 
      SqlDataReader dr = comm.ExecuteReader(); 
      while (dr.Read()) 
      { 
       dropDownList.Items.Add(new 
       ListItem(dr[0].ToString(),dr[1].ToString())); 
      } 
     } 
     catch (Exception e) 
     { 
      lblText.Text = e.Message; 
     } 
     finally 
     { 
      conn.Close(); 
     }