Two DropDownLists getting crossed

J

Jay

I'm having a weird problem in ASP.NET 1.1. I have two DropDownLists in
a form. Both lists are build identically but separately. Somehow the
DropDownLists are getting crossed in memory, because when I set the
SelectedValue for the second list, the first list's SelectedValue
becomes set to the SelectedValue of the second list.

I'm also getting this exception during the render process: "A
DropDownList cannot have multiple items selected." I'm pretty sure this
is related to this problem.

The DDLs are named ddlSupervisorID1 and ddlSupervisorID2. Yes, I am
assigning the second supervisor before the first supervisor. This helps
illustrate the problem better. I'm having the same problem when the
first supervisor is assigned first.)

Any assistance is appreciated.

[begin output]
A: S1 = 0 | S2 = 0
S2: S1 = 0 | S2 = 4787
S1: S1 = 919 | S2 = 919
[end output]

[begin assignment code]

Trace.Write(ID, "A: S1 = " & ddlSupervisorID1.SelectedValue & " | S2 =
" & ddlSupervisorID2.SelectedValue)

'_ Supervisor2
ddlSupervisorID2.SelectedValue = data("SupervisorID2")
Trace.Write(ID, "S2: S1 = " & ddlSupervisorID1.SelectedValue & " | S2 =
" & ddlSupervisorID2.SelectedValue)

'_ Supervisor1
ddlSupervisorID1.SelectedValue = data("SupervisorID1")
Trace.Write(ID, "S1: S1 = " & ddlSupervisorID1.SelectedValue & " | S2 =
" & ddlSupervisorID2.SelectedValue)

[end assignment code]

[begin list construction code]

For Each dr As DataRow In dtAssociates
Dim sKey As String = dr("AssociateNameLF") & " (#" &
dr("AssociateID") & ")"
Dim li As New ListItem(sKey, dr("AssociateID"))
ddlAssociateID.Items.Add(li)
If dr("IsSupervisor") Then
ddlSupervisorID1.Items.Add(li)
ddlSupervisorID2.Items.Add(li)
End If
Next

[end list construction code]
 
N

Norman Yuan

You only created ONE ListItem, but tried to add it to 3 dropdown list. You
should create ListItem for each Dropdownlist:

For Each dr As DataRow In dtAssociates
Dim sKey As String = dr("AssociateNameLF") & " (#" &
dr("AssociateID") & ")"
Dim li As ListIten

li=New ListItem(sKey, dr("AssociateID"))
ddlAssociateID.Items.Add(li)

If dr("IsSupervisor") Then

li=New ListItem(sKey, dr("AssociateID"))
ddlSupervisorID1.Items.Add(li)

li=New ListItem(sKey, dr("AssociateID"))
ddlSupervisorID2.Items.Add(li)

End If
Next
 
J

Jay

Is there a cleaner way to do this? Copying the line "li=New
ListItem(sKey, dr("AssociateID"))" will clutter the code unnecessarily.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top