Ok this is what I have:
-
private List<Car> cars;
-
-
protected void Page_Load(object sender, EventArgs e){
-
if (!IsPostBack)
-
{
-
createCarsSource();
-
theListBox.DataSource = cars;
-
theListBox.DataTextField = "Make";
-
theListBox.DataValueField = "Model";
-
theListBox.DataBind();
-
}
-
}
-
private void createCarsSource() {
-
cars = new List<Car> { };
-
cars.Add(new Car(2010, "Toyota", "Matrix", "Black"));
-
cars.Add(new Car(2010, "GM", "Silverado", "Silver"));
-
cars.Add(new Car(2010, "Honda", "Civic", "Red"));
-
}
Where my Car class is as follows:
-
class Car
-
{
-
private String _colour;
-
private String _make;
-
private String _model;
-
private int _year;
-
-
public String Colour
-
{
-
get { return _colour; }
-
set { _colour = value; }
-
}
-
public String Make
-
{
-
get { return _make; }
-
set { _make = value; }
-
}
-
public String Model
-
{
-
get { return _model; }
-
set { _model = value; }
-
}
-
public int Year
-
{
-
get { return _year; }
-
set { _year = value; }
-
}
-
-
public Car()
-
{
-
}
-
public Car(int year, string make, string model, string colour)
-
{
-
_year = year;
-
_make = make;
-
_model = model;
-
_colour = colour;
-
}
-
}
The "make" of each car is being displayed as text in the ListBox and the "model" for each car is being used for the value.
ASP.NET is creating a ListItem for each car in my collection of cars. The ListItem's text is what I specified for the DataTextField (the "make" of the car) and the ListItem's value is what I specified for the DataValueField (the "model" of the car).
ListItems are very simple. They only have the following properties:
- Attributes: Gets a collection of attribute name and value pairs for the ListItem that are not directly supported by the class.
- Enabled: Gets or sets a value indicating whether the list item is enabled.
- Selected: Gets or sets a value indicating whether the item is selected.
- Text: Gets or sets the text displayed in a list control for the item represented by the ListItem.
- Value: Gets or sets the value associated with the ListItem.
As you can see, you can't exactly store an Object into a ListItem.
It doesn't work that way....
The following code handles the SelectedIndexChanged event for the ListBox.
In it I am retrieving the selected item in the ListBox, re-populating the data source and retrieving the Car Object associated with the selected ListItem:
-
protected void theListBox_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
//Retrieving the selected item
-
ListItem item = theListBox.SelectedItem;
-
String make = item.Text;
-
String model = item.Value;
-
-
//Recreating the data source so that I can retrieve the Car Object
-
//associated with the selected Item
-
createCarsSource();
-
-
//Retrieving the selected Car Object from the data source
-
Car selectedCar = Array.Find(cars.ToArray(), thecar => (thecar.Make == make && thecar.Model == model));
-
-
//msg is a Label on the page...
-
//Displaying the car's details
-
msg.Text = "<br /> Make: " + selectedCar.Make + "<br/> Model: " + selectedCar.Model + "<br/> Colour: " + selectedCar.Colour + "<br/> Year: " + selectedCar.Year.ToString();
-
-
}
Please note that I had to recreate the data source. The reason is because ASP.NET is a stateless environment. That means that the collection of cars that I used as the data source for the page the first time the page was loaded is not available the during the postback unless I recreate it. Every object is created, initialized, modified and disposed of for each request...
You can cache your data source though. You can store it in Session or use ASP.NET's cache. There's a lot of ways to cache the data source...
-Frinny