c# - DropDownList AppendDataBoundItems(第一项为空且无重复项)

标签 c# asp.net drop-down-menu webforms append

我在 UpdatePanel 中有一个 DropDownList,它是在从 SqlDataSource 回发时填充的。它有一个参数,它是另一个控件。有时我需要多次回发,但每次更新面板刷新时,项目都会添加到 DropDownList。因此 DropDownList 最终会包含不正确的数据或重复的数据。

我将 AppendDataBoundItems 属性设置为 true 因为我需要第一个项目为空。

我该如何克服这个问题?还有另一种方法可以让第一项为空吗?

(此 DropDownList 在 ASP.NET 2.0 网络应用程序中,代码隐藏在 C# 中)

最佳答案

不要使用 AppendDataboundItems='true'(这会导致您正在谈论的问题),而是响应 DropDownListDataBound 事件,然后将您的“空白”项目添加到列表的顶部。

<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/

相关文章:

CSS下拉菜单问题

c# - 对象建模问题

c# - 在 .NET 的 UI 测试自动化框架中实现剧本模式/ Actor 模型

c# - 无法读取鼠标事件处理程序内部WPF中的键的保留状态-VMWare Fusion for Mac上Windows guest虚拟机中的行为不一致

c# - 如果端点已经在运行,如何确保端点不执行该方法?

c# - ASP.NET MVC ModelState 如何重新运行验证

c# - 获取 Oracle 数据库名称

javascript - 在 iframe 托管代码无法访问的外部网站时获取 iframe 内容的高度

html - CSS下拉菜单跳转

html - 子导航栏下拉菜单不起作用