Maintaining DropDownList selected index

Discussion in 'ASP .Net' started by Phuff, Apr 10, 2006.

  1. Phuff

    Phuff Guest

    Thanks in advance! I'm trying to maintain a dl list's selected index
    on postback. What I'm doing is when a person selects an item from the
    drop down list I select a date in a calendar control and populate a
    text box with the date. To do this I set auto postback to the drop
    down list. But it always resets the selected index on post back. I
    control for this in the onLoad method...

    If Not Page.IsPostBack Then

    If page is posted back I select the date and populate textbox.text
    correctly. Selected index is lost. I even added a key to the
    viewstate with the selected index and tried grabbing the value from
    that, but still no go. Help please!
    Phuff, Apr 10, 2006
    #1
    1. Advertising

  2. Drop down lists do usually maintain their viewstate unless you re-bind them
    to their datasource. Howeve i have found that if your DDL is bound to a
    DataSourceControl, and your DDL is within another control (such as in a
    template field of a DetailsView), then if you do something such as updatting
    the headerText of one of the Fields in the detailsview control then it
    re-sets the view state of all child controls within parent control.

    "Phuff" wrote:

    > Thanks in advance! I'm trying to maintain a dl list's selected index
    > on postback. What I'm doing is when a person selects an item from the
    > drop down list I select a date in a calendar control and populate a
    > text box with the date. To do this I set auto postback to the drop
    > down list. But it always resets the selected index on post back. I
    > control for this in the onLoad method...
    >
    > If Not Page.IsPostBack Then
    >
    > If page is posted back I select the date and populate textbox.text
    > correctly. Selected index is lost. I even added a key to the
    > viewstate with the selected index and tried grabbing the value from
    > that, but still no go. Help please!
    >
    >
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 10, 2006
    #2
    1. Advertising

  3. Phuff

    Phuff Guest

    Thanks, but its not bound. What I do is loop through dataset grabbed
    from a sql query, and then do an Items.add method.

    I have actually found that it will maintain 2 selected states. One
    labeled 7 and 1. The first index is a text field describing all items.
    Then I add the other fields which are numbers. The value is also set
    to equal the number listed as a string in the field. OIn my test
    example I have a string..."Box/Unit ID" and then an item for 1-8. If I
    select 7 it will actually keep this index. Everything else defaults to
    1.
    Phuff, Apr 10, 2006
    #3
  4. Why not just bind it instead, then it will maintain ViewState?

    "Phuff" wrote:

    > Thanks, but its not bound. What I do is loop through dataset grabbed
    > from a sql query, and then do an Items.add method.
    >
    > I have actually found that it will maintain 2 selected states. One
    > labeled 7 and 1. The first index is a text field describing all items.
    > Then I add the other fields which are numbers. The value is also set
    > to equal the number listed as a string in the field. OIn my test
    > example I have a string..."Box/Unit ID" and then an item for 1-8. If I
    > select 7 it will actually keep this index. Everything else defaults to
    > 1.
    >
    >
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 10, 2006
    #4
  5. Phuff

    Phuff Guest

    Ok, I'll try it.. getting nothing on the bind... is this the correct
    way to do so? (As you can see I commented out part of the old way)

    Dim sqlSel As String = "SELECT Box_Id FROM Job_Order_Box WHERE
    Order_ID=" + txtHidden.Text

    'create new datasets and data adapters
    Dim dsBox As New DataSet
    Dim daBox As New OdbcDataAdapter(sqlSel, strCon)

    'fill first dataset and populate drop down list with engineers
    daBox.Fill(dsBox)

    'dlBox.Items.Add("All Units/Boxes")
    'dlBox.Items(0).Value = "0"
    dlBox.DataSource = dsBox.Tables(0).DefaultView
    Phuff, Apr 10, 2006
    #5
  6. Phuff

    Phuff Guest

    Ok got it figured out...it is databound.


    I still have the issue.

    I select Box_Id column and fill the ddl. On the particular order I'm
    processing I have 1-8. Everything under 7 defaults to 1, and 7 and 8
    default to 7. Its bizzare. It will only get those two indices.
    Phuff, Apr 10, 2006
    #6
  7. You need to set the DataTextField and DataValueField to the relevent fields.
    You also have to call the DataBind method, but make sure you put it in a
    (sorry i don't speak VB) if (!IsPostBack) section, so that it does not
    re-bind everytime the page is posted back.




    "Phuff" wrote:

    > Ok, I'll try it.. getting nothing on the bind... is this the correct
    > way to do so? (As you can see I commented out part of the old way)
    >
    > Dim sqlSel As String = "SELECT Box_Id FROM Job_Order_Box WHERE
    > Order_ID=" + txtHidden.Text
    >
    > 'create new datasets and data adapters
    > Dim dsBox As New DataSet
    > Dim daBox As New OdbcDataAdapter(sqlSel, strCon)
    >
    > 'fill first dataset and populate drop down list with engineers
    > daBox.Fill(dsBox)
    >
    > 'dlBox.Items.Add("All Units/Boxes")
    > 'dlBox.Items(0).Value = "0"
    > dlBox.DataSource = dsBox.Tables(0).DefaultView
    >
    >
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 10, 2006
    #7
  8. Phuff

    Phuff Guest

    Ya I did that, and I get the control as databound no problem. But it
    is still defaulting to those values. Actually its the 0 index and 6
    index. Sure databound works just as well as my other method did, but
    the selected index is not being maintained correctly. Is this a .net
    2.0 issue?
    Phuff, Apr 10, 2006
    #8
  9. Phuff

    Phuff Guest

    I also did some javascript to get the index client side and see what it
    was. the ddl will only return 2 indices out of the 7 available. If I
    set the selected index initially it will be selected correctly. I
    select a new item and it then defaults to one of the two indices. So
    bizzare. Never had an issue like this before.
    Phuff, Apr 10, 2006
    #9
  10. Phuff

    Phuff Guest

    Also, I have another DDL on the page that I fill in the same manner and
    it doesn't have these issues. I will keep digging.
    Phuff, Apr 10, 2006
    #10
  11. Phuff

    Phuff Guest

    Problem solved! Apparently if you have any duplicates in your Value
    collection the selected index that is maintained through the state
    defaults to the first index in that group of duplicates.

    So my items correspond to the Box_Id in the table (the unique
    identifier in the DB). There are 8 boxes, 1-8. Then I grab a business
    related date and fill the Value collection with this, that date
    corresponds to something with each box. The dates (which become
    values) could all be different or the same, there is no consistency
    with the dates. Well boxes 1-6 all had the same recieve date so the
    value was the same for all those items. Therefore, if you select
    anything within "Index" 0-5 it will default to 0 because that is the
    first item in the list with the duplicated date. Same for "Index" 6-7.
    So when it is maintaining its state it remembers the selected index,
    but won't allow duplicates in the value field. They should provide a
    property which allows duplicates in the value field."

    Phuff wrote:
    > Also, I have another DDL on the page that I fill in the same manner and
    > it doesn't have these issues. I will keep digging.
    Phuff, Apr 10, 2006
    #11
    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. Iain
    Replies:
    3
    Views:
    914
  2. Nathan Sokalski
    Replies:
    0
    Views:
    593
    Nathan Sokalski
    Oct 5, 2008
  3. mldardy
    Replies:
    0
    Views:
    950
    mldardy
    Sep 28, 2010
  4. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    280
    Nathan Sokalski
    Oct 5, 2008
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    266
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page