如何将标准值放在下拉列表的顶部

时间:2012-06-13 09:06:31

标签: c# asp.net

我的下拉列表将填充来自实体模型的数据,如下所示:

    HotelTestDatabaseEntities hotelData = new HotelTestDatabaseEntities();
    var afdelingQuery = from afd in hotelData.Afdelings
                        orderby afd.afdelingNaam
                        select afd;
    DropDownList1.DataValueField = "afdelingID";
    DropDownList1.DataTextField = "afdelingNaam";
    DropDownList1.DataSource = afdelingQuery;
    DataBind();

如何在下拉列表顶部放置“选择值...”项?

3 个答案:

答案 0 :(得分:4)

为DropDownList1完成绑定后添加此代码。

DropDownList1.Items.Insert(0,new ListItem("Please Select One","0"));

答案 1 :(得分:2)

在您的aspx页面

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value="0">Select Value</asp:ListItem>
</asp:DropDownList>

并在代码中

DropDownList1.AppendDataBoundItems = true;
DropDownList1.DataValueField = "afdelingID";
DropDownList1.DataTextField = "afdelingNaam";
DropDownList1.DataSource = afdelingQuery;
DataBind();

答案 2 :(得分:0)

这就是我的想法,这对我而言

     int i = 0;
     foreach (DataRow dr in dt.Rows)
       {
           if (i == 0)
           {
               comboBox1.SelectedText = dr[0].ToString();
           }
           else
           {

               comboBox1.Items.Add(dr[0].ToString());
           }
           i++;

       }