Problem with setting DropDownList selectedindex on multiple controls

Discussion in 'ASP .Net' started by David Austin via .NET 247, Jun 5, 2004.

  1. Hello, I hope this is where I should post this.
    I have a problem with this code that I can't figure out...seems to be a bug in the dropdownlist control.
    I am trying to set multiple dropdownlists to match retrieved data. When I set the first, everything is fine. Then I set the second and the first gets set to the second index. Can you see anything wrong with this code? Thanks

    If DR.HasRows Then
    Do While DR.Read()
    Dim name As String = DR.Item("position").ToString
    Dim Type As String = DR.Item("Type").ToString
    Dim Value As String = DR.Item("PositionID").ToString

    Select Case Type.ToUpper
    Case "ACMC"
    Dim ACMCselectedItem As New System.Web.UI.WebControls.ListItem
    ACMCselectedItem = cboACMC.Items.FindByValue(Value)
    If Not (ACMCselectedItem Is Nothing) Then
    cboACMC.SelectedIndex = cboACMC.Items.IndexOf(ACMCselectedItem)
    End If

    Case "CASAG"
    Dim CASAGselectedItem As New System.Web.UI.WebControls.ListItem
    CASAGselectedItem = cboCASAG.Items.FindByValue(Value)
    If Not (CASAGselectedItem Is Nothing) Then

    cboCASAG.SelectedIndex = cboCASAG.Items.IndexOf(CASAGselectedItem) <<<--- (this step resets cboACMC.SelectedIndex as well!!! I have watched it happen in debug.)

    End If
    Case "DESK"
    Dim item As New System.Web.UI.WebControls.ListItem
    item.Text = name
    item.Value = DR.Item("PositionID").ToString
    lstDeskOffices.Items.Add(item)
    End Select
    Loop
    End If

    thanks for any help you can give me..

    --------------------------------
    From: David Austin

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>04vPTV01T0mbVXp08ODeUw==</Id>
     
    David Austin via .NET 247, Jun 5, 2004
    #1
    1. Advertising

  2. It's not a bug. This behavior is by design - poorly understood al beit.

    The work around is to have separate items in the list. Here, you have two
    dropdownlist boxes pointing to the same object.

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "David Austin via .NET 247" <> wrote in message
    news:%...
    > Hello, I hope this is where I should post this.
    > I have a problem with this code that I can't figure out...seems to be a
    > bug in the dropdownlist control.
    > I am trying to set multiple dropdownlists to match retrieved data. When I
    > set the first, everything is fine. Then I set the second and the first
    > gets set to the second index. Can you see anything wrong with this code?
    > Thanks
    >
    > If DR.HasRows Then
    > Do While DR.Read()
    > Dim name As String = DR.Item("position").ToString
    > Dim Type As String = DR.Item("Type").ToString
    > Dim Value As String = DR.Item("PositionID").ToString
    >
    > Select Case Type.ToUpper
    > Case "ACMC"
    > Dim ACMCselectedItem As New
    > System.Web.UI.WebControls.ListItem
    > ACMCselectedItem =
    > cboACMC.Items.FindByValue(Value)
    > If Not (ACMCselectedItem Is Nothing) Then
    > cboACMC.SelectedIndex =
    > cboACMC.Items.IndexOf(ACMCselectedItem)
    > End If
    >
    > Case "CASAG"
    > Dim CASAGselectedItem As New
    > System.Web.UI.WebControls.ListItem
    > CASAGselectedItem =
    > cboCASAG.Items.FindByValue(Value)
    > If Not (CASAGselectedItem Is Nothing) Then
    >
    > cboCASAG.SelectedIndex =
    > cboCASAG.Items.IndexOf(CASAGselectedItem) <<<--- (this step resets
    > cboACMC.SelectedIndex as well!!! I have watched it happen in debug.)
    >
    > End If
    > Case "DESK"
    > Dim item As New
    > System.Web.UI.WebControls.ListItem
    > item.Text = name
    > item.Value = DR.Item("PositionID").ToString
    > lstDeskOffices.Items.Add(item)
    > End Select
    > Loop
    > End If
    >
    > thanks for any help you can give me..
    >
    > --------------------------------
    > From: David Austin
    >
    > -----------------------
    > Posted by a user from .NET 247 (http://www.dotnet247.com/)
    >
    > <Id>04vPTV01T0mbVXp08ODeUw==</Id>
     
    Alvin Bruney [MVP], Jun 6, 2004
    #2
    1. Advertising

  3. And to add to what has already been said, You should take care as to not to add the same item to Different DropdownList Boxes lik

    Dim lstItem as New ListItem("One", "1"
    ddlItem1.Items.Add(lstItem
    ddlItem2.Items.Add(lstItem

    Regards

    Trevor
     
    =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?=, Jun 7, 2004
    #3
    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. K Bryan
    Replies:
    4
    Views:
    2,457
    Andrew de la Harpe
    Apr 15, 2004
  2. keithb
    Replies:
    1
    Views:
    12,000
    shauns1
    Nov 1, 2006
  3. Jason Wilson
    Replies:
    1
    Views:
    13,287
    Ken Cox [Microsoft MVP]
    Sep 14, 2006
  4. S_K
    Replies:
    5
    Views:
    770
    Eliyahu Goldin
    Nov 14, 2007
  5. wrecker
    Replies:
    1
    Views:
    180
    Wardeaux
    Feb 13, 2004
Loading...

Share This Page