DropDownList forgets SelectedIndex

Discussion in 'ASP .Net Web Controls' started by Fred Sawtelle, Sep 27, 2004.

  1. I have a DropDownList which is databound on first page
    load to a dataview, and which has its AutoPostback set to
    true (yes, this is necessary). For each item in the
    list, the text is either a department name or an alias
    for a department, and the value is a DeptID. Departments
    and their respective aliases have the same DeptID in the
    value field.

    My DropDownList appears to be saving its SelectedValue in
    the ViewState for server trips. I thought it would be
    SelectedIndex. But I have found that when an item is
    selected which shares its value with other items, the
    first item alphabetically by text field is the one
    selected after a server trip.

    For example, an item may have a value of 20 and a text
    of "Regular Department Name", and another item may have a
    value of 20 and a text of "Alias Department Name". If I
    select "Regular Department Name" in the list, it performs
    its AutoPostback, and when the page loads it sets itself
    to "Alias Department Name". However, if I
    preface "Regular" with "AAA" (making it first
    alphabetically), it doesn't do this. If I
    preface "Alias" with "RRR" (making it second
    alphabetically), it doesn't do it.

    I've seen this behavior with several different examples.
    The simple explanation would be that it's just looking
    for the first occurrence of its SelectedValue and setting
    SelectedIndex to that.

    I need it to remember the actual SelectedIndex. I should
    mention that this control is part of a server control
    that is rendered in a portal, and its name has been
    randomly changed by the time it makes it to HTML at the
    client, so getting the SelectedIndex from Request.Form is
    not an option.

    Any help with this would be appreciated.

    Fred Sawtelle
     
    Fred Sawtelle, Sep 27, 2004
    #1
    1. Advertising

  2. Fred Sawtelle

    Alex Homer Guest

    ASP.NET can only tell which item is selected by looking at the contents of
    the Request collections (Request.Form usually). The viewstate only tells it
    what was selected when the page was created and sent to the browser (this is
    how it knows if a value has changed and so it can fire the appropriate
    OnXxxxChanged event). HTML rules and browser behavior result in a <select>
    element posting back the currently selected value if there is a value
    attribute for that <option>, or the text of the <option> element if there is
    no value attribute. So you need to ensure that the value of each item in the
    list is unique or you'll get the wrong answer...
     
    Alex Homer, Sep 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eidolon
    Replies:
    2
    Views:
    360
    Eidolon
    Oct 17, 2003
  2. R-D-C
    Replies:
    2
    Views:
    358
    Joe Fallon
    Apr 24, 2004
  3. Jaikanth Sithambaranathan via .NET 247

    Browser refresh forgets the original request

    Jaikanth Sithambaranathan via .NET 247, Mar 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    703
    bruce barker
    Mar 9, 2005
  4. keithb
    Replies:
    1
    Views:
    12,168
    shauns1
    Nov 1, 2006
  5. S_K
    Replies:
    5
    Views:
    803
    Eliyahu Goldin
    Nov 14, 2007
Loading...

Share This Page