Results 1 to 3 of 3

Thread: [RESOLVED] Databound DropDownList's SelectedValue is always "1"..?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] Databound DropDownList's SelectedValue is always "1"..?

    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!

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Databound DropDownList's SelectedValue is always "1"..?

    Ok... Nevermind, that was stupid. I forgot to check IsPostBack in the page load event, so as soon as I clicked the button the dropdowns are re-loaded and the selected index is set back to 0, making the selected value indeed "1" for the first car.

    D'oh!


    I'd still like to know if there is any way to load the actual Car objects into the dropdown? It seems a little strange to load all cars first, and then load one of them from the database a second time by id... I'm sure I can put the cars in a global List(of Car) and extract one car by id from there, but it would be useful if I could put the cars in directly.

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Databound DropDownList's SelectedValue is always "1"..?

    Hey,

    What it all comes down to is that an ASP.Net Page is stateless. Now, there is the ViewState that allows you to maintain information about a page across post backs, etc, however, as you will have probably read, ASP.Net ViewState can become huge and cause problem with page loads etc. So, where possible, you don't want to keep stuff around when it is not needed.

    What this means is that some of the paradigms between web forms and windows form just don't go together. i.e. as you have said, you would have expected to be able to cast the object in the drop down list to type car, and get all the information about it, but that doesn't really apply here.

    There are techniques that you can use though, like Caching:

    http://msdn.microsoft.com/en-us/libr...8c(VS.71).aspx

    Either at the page level, or object level, which means you don't always have to go back to the database, you can jump pull the object from the cache, based on the ID, and use that. Typically, I add this logic into my BLL layer, and make a decision in there about whether I am going to pull the object from the Cache or not.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width