Using asp.net webforms I need to store additional data (other than the value
field and the text
field) in each list item.
Here's my datasource:
class Person
{
public string Name {get;set;}
public string PersonId {get;set;}
public string PersonType {get;set;}
}
List<Person> lista = GetPersons();
ListBox1.DataTextField = "Name";
ListBox1.DataValueField = "PersonId";
ListBox1.DataSource = lista;
ListBox1.DataBind();
The DataTextField
is the Name. The DataValueField
is the PersonId but I also want to bind the PersonType property so that when I retrieve the selected item from the user on the page postback:
ListItemCollection items = ListBox1.Items;
foreach (ListItem item in items)
{
if (item.Selected == true)
{
// Here I want to retrive also
// the PersonType attribute
string personType = item.????
}
}
How can I achieve that?