如何只清除ASP.NET DropDownList中的数据绑定项目,而不是在源代码中添加任何项目?(How to clear ONLY the databound items from a ASP.NET DropDownList and NOT any items added in Source?)

标题几乎问了整个问题 - 我如何才能清除ASP.NET DropDownList中的数据绑定项目,而不是在源代码中添加任何项目? 如果我有以下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都会消失,下一次我重新绑定时,“(请选择)”默认选项就消失了。

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

谢谢!

The title pretty much asks the whole question - How can I clear ONLY the databound items from a ASP.NET DropDownList and NOT any items added in Source? If I have the following ddl:

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

...and then call the following:

ddl1.Items.Clear()

The all the DDL ListItems go away and next time I rebind, the "(Please Select)" default option is gone.

Is there a more streamlined or elegant way to clear only the databound items but leave the "(Please Select)" default ListItem added in the source? I think I could call the Items.Clear() and pop that default item back in like ddl1.Items.Add(New ListItem("(Please Select)", "0")) as a new ListItem object, but I wondered if there was a better way to do this?

Thanks!

最满意答案

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

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

I found no other streamlined or better way than clearing the list and then adding the default item back into the collection like shown below:

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

更多推荐

ddl,ListItem,Select,Items,电脑培训,计算机培训,IT培训"/> <meta name="des