添加到链接到 SqlDataSource 的下拉列表

问题描述 投票:0回答:1

我有一个链接到 SQL 表的下拉列表。该表没有空行,因此当我加载下拉列表时,它显示 SQL 表中的第一个条目。我需要下拉列表的第一个条目为空。我尝试了下面的方法,但它仍然显示 SQL 表中的第一项。

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DropDownList33.Items.Insert(0, new ListItem(" ", " "));
    }
}
c# dropdownlistfor
1个回答
0
投票

设置

AppendDataBoundItems=true
,然后将空项目添加到
ListItem
,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DropDownList33.AppendDataBoundItems = true;
        DropDownList33.Items.Insert(0, new ListItem(String.Empty, String.Empty));
        DropDownList33.SelectedIndex = 0;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.