我在 UpdatePanel
中有一个 DropDownList
,它是在从 SqlDataSource
回发时填充的。它有一个参数,它是另一个控件。有时我需要多次回发,但每次更新面板刷新时,项目都会添加到 DropDownList
。因此 DropDownList
最终会包含不正确的数据或重复的数据。
我将 AppendDataBoundItems
属性设置为 true
因为我需要第一个项目为空。
我该如何克服这个问题?还有另一种方法可以让第一项为空吗?
(此 DropDownList
在 ASP.NET 2.0 网络应用程序中,代码隐藏在 C# 中)
最佳答案
不要使用 AppendDataboundItems='true'
(这会导致您正在谈论的问题),而是响应 DropDownList的
,然后将您的“空白”项目添加到列表的顶部。DataBound
事件
<asp:DropDownList runat="server" ID="MyList"
ondatabound="MyListDataBound"></asp:DropDownList>
然后在你后面的代码中:
protected void MyListDataBound(object sender, EventArgs e)
{
MyList.Items.Insert(0, new ListItem("- Select -", ""));
}
关于c# - DropDownList AppendDataBoundItems(第一项为空且无重复项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/730555/