Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropdownList problem

Status
Not open for further replies.

wbeetge

Programmer
May 2, 2002
57
ZA
I have a DropDownlist on my ASPX form. I need to populate another DropDownList when I make a selection from the first one.
As soon as the code behind triggers on the SelectedIndexChanged event, the selected item resets to Item(0). Trying to use the value of the selected item in the code then always uses the first listitem, and not the last selected item.
How do I stop this from happening when code-behind is executed ?
 
wb: First impression is of course the SelectedItem should be available on post-back. Here is a couple of snippets from an aspx page where I carry out the same operation:

Code:
..page load, populate first dd
If Not IsPostBack Then
 ddWID.Items.Add(New ListItem("Alabama", "01"))
 ddWID.Items.Add(New ListItem("Cahaba", "02"))
 ddWID.Items.Add(New ListItem("Chattahoochee", "03"))
 ...
 ddWID.Items.Add(New ListItem("Tennessee", "08"))
 ddWID.Items.Add(New ListItem("Tombigbee", "09"))
 ddWID.Items.Add(New ListItem("Warrior", "10"))
 GetGroups()
End If

..in which case the first item up is [0], and the Function GetGroups() is called to populate the second dd, q.v., 

Private Sub GetGroups()      
'open database...
Dim cmdSelect As OLEDbCommand
Dim dbconn As OleDbConnection = New OleDbConnection( _
  "Provider=Microsoft.Jet.OLEDB.4.0; " & _
  "Data Source=" & Server.MapPath("fpdb\Groups.mdb;"))  
cmdSelect = New OLEDbCommand("SELECT _
WebMasterGroups.GrpID, WebMasterGroups.Group_Name FROM _
WebMasterGroups INNER JOIN tblAwwCode ON _
WebMasterGroups.GrpID = tblAwwCode.GrpID WHERE _
Left([tblAwwCode].[AwwCode],2)='" & ddWID.SelectedItem.Value & "'" & " ORDER BY _
WebMasterGroups.Group_Name", dbconns)
 dbconn.Open() 
 ddGroups.DataSource = cmdSelect.ExecuteReader()
 ddGroups.DataTextField = "Group_Name"
 ddGroups.DataValueField = "GrpID"
 ddGroups.DataBind()  
 dbconn.Close()       
End Sub

..this sets up the first two options (I have a "button" next to these to call information from the 2d dropdown). Note that the SQL uses the "SelectedItem.Value" of the first dropbox. The repopulation occurs without a problem; and AutoPostBack is set to True for the first dd.

Post your code, and we'll test it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top