asp.net - 如何仅清除 ASP.NET DropDownList 中的数据绑定(bind)项,而不清除 Source 中添加的任何项?

标签 asp.net drop-down-menu

标题几乎提出了整个问题 - 如何仅清除 ASP.NET DropDownList 中的数据绑定(bind)项,而不清除 Source 中添加的任何项?如果我有以下 ddl:

<asp:DropDownList ID="ddl1" runat="server" 
                  Width="300px" AppendDataBoundItems="true">
    <asp:ListItem Text="(Please Select)" Value="0" />
</asp:DropDownList>

...然后调用以下命令:

ddl1.Items.Clear()

所有 DDL ListItems 消失了,下次我重新绑定(bind)时,“(请选择)”默认选项消失了。

是否有一种更精简或更优雅的方式来清除数据绑定(bind)项,但保留源中添加的“(请选择)”默认 ListItem?我想我可以调用 Items.Clear() 并将默认项目像 ddl1.Items.Add(New ListItem("(Please Select)", "0")) 作为新的ListItem 对象,但我想知道是否有更好的方法来做到这一点?

谢谢!

最佳答案

我发现没有其他精简或比清除列表然后将默认项添加回集合中更好的方法,如下所示:

ddl1.Items.Clear()
ddl1.Items.Add(New ListItem("(Please Select)", "0"))

关于asp.net - 如何仅清除 ASP.NET DropDownList 中的数据绑定(bind)项,而不清除 Source 中添加的任何项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510435/

相关文章:

Javascript 控件,例如 Google+ 可见性下拉菜单

javascript - Jquery仅获取Bootstrap中选定的项目

javascript - 从 C# Web 应用程序背后的代码调用 javascript 函数

c# - 静态变量生命周期和应用程序池回收

c# - RadGrid 列在插入时可编辑但在更新时设置为只读

mysql - 如何匹配另一个表中的 ID

asp.net - 如何在 Windows Azure 上禁用 ASP.NET v4.0 Extensionless URL 功能

jquery - 显示相同内容的两个下拉菜单

CSS:下拉菜单 float 在顶部

javascript - 带有巨型下拉菜单的两条水平线