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!
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: