2

ドロップダウンには、次のようにエンティティモデルからのデータが入力されます。

    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();

ドロップダウンリストの一番上に「値の選択...」アイテムを配置するにはどうすればよいですか?

4

3 に答える 3

4

DropDownList1のバインドが完了したら、このコードを追加します。

DropDownList1.Items.Insert(0,new ListItem("Please Select One","0"));
于 2012-06-13T09:08:46.997 に答える
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();
于 2012-06-13T09:12:35.077 に答える
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++;

       }
于 2013-12-16T10:43:59.347 に答える