Problem with setting DropDownList selectedindex on multiple controls

  • Thread starter David Austin via .NET 247
  • Start date
D

David Austin via .NET 247

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..
 
A

Alvin Bruney [MVP]

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.
 
G

Guest

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top