我正在使用数据表对我的下拉列表进行数据绑定。我需要在下拉列表中再添加一个项目,该项目需要成为选择中的第一个项目。我尝试了很多方法,但没有任何效果。
ddlCountry.Items.Add("--Select--");
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
您的页面声明:
<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();
尝试这个
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("Select Country", "0"));
添加前先绑定数据
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
ddlCountry.Items.Add("--Select--");