Hi,

I am quite new to ASP.NET but I think I'm managing so far. I have now come across an odd problem that I can't seem to solve on my own...

I have a DropdownList control on a page where the user has to select a car. The available cars come from a database, and I am loading them into a List(Of Car), where Car is a simple class with four properties (Id, Team, Engine, Type), one readonly property that returns the text representation (so I can change this easily):
vb.net Code:
  1. Public Class Car
  2.  
  3.         Public Sub New(ByVal id As Integer, ByVal team As String, ByVal engine As String, ByVal type As String)
  4.             Me.Id = id
  5.             Me.Team = team
  6.             Me.Engine = engine
  7.             Me.Type = type
  8.         End Sub
  9.  
  10.         Public Property Id As Integer
  11.         Public Property Team As String
  12.         Public Property Engine As String
  13.         Public Property Type As String
  14.  
  15.         Public ReadOnly Property Text
  16.             Get
  17.                 Return String.Format("{0} {1} ({2})", Me.Team, Me.Type, Me.Engine)
  18.             End Get
  19.         End Property
  20.  
  21.         Public Overrides Function ToString() As String
  22.             Return Me.Text
  23.         End Function
  24.  
  25.     End Class

Now in the Page_Load event I call the LoadCars method which fills the DropdownList:
vb.net Code:
  1. Private Sub LoadCars()
  2.         Dim carManager As New CarManager
  3.         Dim cars As List(Of Car) = carManager.Load()
  4.         cboCars.DataSource = cars
  5.         cboCars.DataTextField = "Text"
  6.         cboCars.DataValueField = "Id"
  7.         cboCars.DataBind()
  8.         cboCars.SelectedIndex = 0
  9.     End Sub
The CarManager.Load method merely loads all cars from the database into a List(Of Car). As you can see, I set the DataTextField to "Text" (so that it uses the text representation of my choice) and the DataValueField to "Id", so that the actual values are the Id's of the cars.


In normal VB.NET (winforms) I would now expect the DropdownList to actually hold instances of the Car class, and that I can get the selected instance by using
vb.net Code:
  1. Dim car = DirectCast(cboCars.SelectedItem, Car)
But it seems this doesn't work that way in ASP.NET. The SelectedItem is a ListItem which cannot be cast to a Car.

Ok, that's a bit of a setback, but no worries, I can still get the Id via the SeletedValue property. Or so I thought... Apparently not

I would expect the SelectedValue property to return the value of the field I set as the DataValueField. This way I can still get the selected Car by loading it by Id:
vb.net Code:
  1. Dim car = carManager.LoadById(CInt(cboCars.SelectedValue))
Since I set the DataValueField to "Id" I would expect the SelectedValue to hold the Id of my Car.
But, for some reason, the SelectedValue is always "1" (the string "1", not an integer even). I've no idea why this happens. The Id property of my cars are not all 1, they go from 0 to 19. Yet, the SelectedValue always returns "1" and I keep getting the same car back no matter which I select.


What's going on, and how do I retrieve either the instance of the Car in the dropdown, or the Id of that instance if that is impossible directly?

Thanks!