I have this unexpected behaviour with an ASP.NET web forms DropDownList. I am adding ListItems to a drop down list, and all looks well. Except that the value in the ddl (which in my case is the Id) is never set, it is replaced by the text from the ListItem.
<asp:DropDownList ID="ddl1" runat="server">
</asp:DropDownList>
private void Populate()
{
List<ListItem> list = new List<ListItem>();
foreach (var item in GetItems())
{
list.Add(new ListItem(item.name, item.id.ToString()));
//in the drop down list the ListItem.Value is being replaced
//by ListItem.Text when it is added to the ddl
}
ddl1.DataSource = list;
ddl1.DataBind();
ddl1.Items.Add(new ListItem("Make a selection"));
}
private List<Stuff> GetItems()
{
List<Stuff> list = new List<Stuff>();
list.Add(new Stuff{ name = "bill", id = 1});
list.Add(new Stuff{ name = "james", id = 2});
return list;
}
private struct Stuff
{
public string name;
public int id;
}
Any ideas if this is what is meant to happen? And if it is how do I store both a name and an Id in the ddl?
In your populate method, you are data binding, but not specifying which property should bind to which value in your list of list items.
To do this you need to set the DataTextField and DataValueField properties.
You might find it simpler to change your Populate code to the following:
Or even:
i.e. Just add your items to the DropDownList directly.
In your original example, to use databinding, there’s no need to create a List of ListItem. You can databind stuff directly; in this instance, your code becomes:
i.e. you can databind direct to a list of stuff as long as you tell the DropDownList where to get it’s values from. Many people prefer this model as the SelectedItem is then a Stuff, rather than a string, so you have the object representing the item directly. The disadvantage is that your ViewState just got bigger 😉