Love hate relationship with Asp:DropDownList.AppendDataBoundItems
Here’s the deal.
I want to bind the dropdownlist to a List
But I want the dropdownlist to have an initial empty field.
Most people do this:
<asp:DropDownList … EnableViewState=“true” AppendDataBoundItems=“true”>
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
Which is nice and dandy, until you need to update the datasource of the dropdownlist.
AppendDataBoundItems will make sure all the current items stays in the list, and all the ones in the new datasource gets added to the list.
So you ended up writing silly code that looks like this:
string selectedValue = dropdownlist.SelectedValue;
dropdownlist.Items.Clear();
dropdownlist.Items.Add(new ListItem());
dropdownlist.DataSource= List
dropdownlist.DataBind();
dropdownlist.selectedValue = selectedValue;
I promise my next code rant will look a lot better than this rubbish!
Discussions