我正在将我的下拉列表与一个数据表进行数据绑定。我需要在下拉列表中添加一个项目,该项目必须是选择的第一个项目..

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

10-04 13:51