1

我正在使用数据表对我的下拉列表进行数据绑定。我需要在下拉列表中再添加一个项目,该项目需要成为选择中的第一个项目。我尝试了很多方法,但没有任何效果。

ddlCountry.Items.Add("--Select--");
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
4

3 回答 3

4

您的页面声明:

<asp:dropdownlist id="ddlCountry" AppendDataBoundItems="true" runat="server" >
    <asp:ListItem Value="0" Text="[ Select ]" Selected="True"></asp:ListItem>
</asp:dropdownlist>

AppendDataBoundItems="true" is the key.

您背后的代码:

ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
于 2013-07-22T09:20:16.957 回答
4

尝试这个

 ddlCountry.DataTextField = "Country";
 ddlCountry.DataValueField = "Country";
 ddlCountry.DataSource = dt;
 ddlCountry.DataBind();
 ddlCountry.Items.Insert(0, new ListItem("Select Country", "0"));
于 2013-07-22T09:44:32.230 回答
2

添加前先绑定数据

ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
ddlCountry.Items.Add("--Select--");
于 2013-07-22T09:18:24.360 回答