I have this code
ListItem item=new ListItem();
item.Text="foo";
item.Value="1";
ListItem item2=new ListItem();
item2.Text="bar";
item2.Value="2";
List<ListItem> foobar=new List<ListItem>();
foobar.Add(item); foobar.Add(item2);
foreach(ListItem i in foobar)
{
lblPrintInfo.Text+="Text:"+i.Text+" Value: "+i.Value;
}
And I expect to get Text:foo Value:1 Text:bar Value:2 like result, but actually I get Text:foo Value:foo Text:bar Value:bar. Where am I wrong?
What you need to do is set the
DataValueFieldandDataTextFieldattributes:Add those lines before the
listB.DataBind();call: