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 goes to first record on PostBack

Status
Not open for further replies.

bubberz

Programmer
Dec 23, 2004
117
US
I have two dropdownlists. The second is populated from the first ddl selection, hence the record selection is filtered via the first ddl to the second ddl.
I need to have Postback on the second to populate a third ddl. If I select the third item in the second ddl, the postback happens and this ddl goes back to the first record, and doesn't stay @ the third selected item.

I build this second ddl first in:
Page_Load
If (Not Page.IsPostBack).....

I also re-build this second ddl in:
Page_PreRender
Dim sqlGrp As String
sqlGrp = "SELECT GC+' - '+GD as Expr2, GC,GD, "
sqlGrp &= "DC FROM [RL-DG] WHERE "
sqlGrp &= "DC = " & " '" & ddlDivision.SelectedValue.ToString() & "'"
Dim cmdGrp As New OleDbCommand(sqlGrp, Con1)
Con1.Open()
Dim myRDR As OleDbDataReader = cmdGrp.ExecuteReader()
If myRDR.HasRows = True Then
ddlGroup2.DataSource = myRDR
ddlGroup2.DataTextField = "Expr2"
ddlGroup2.DataValueField = "GD"
ddlGroup2.DataBind()
End If
Con1.Close()

Any suggestion would be much appreciated!
Thanks!
 
first off...are all your ddl's enabledviewstate = true?


page load
if not page.ispostback then
Load first ddl
end if


Private Sub ddl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddTypes.SelectedIndexChanged
load ddl2 based on this selection
End Sub

Private Sub ddl2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddTypes.SelectedIndexChanged
load ddl3 based on this selection
End Sub

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Hey checkai!
Yes, they are both set to enableviewstate="true"

I tried your suggestion, and still have the second ddl, ddlGroup2, resetting back to the first option and not keeping it's selected value when it's doing a PostBack.
I'm now trying the following code:

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
Dim sDiv As String = ""
Dim sGrp As String = ""
Dim sRes As String = ""

If ddlDivision.SelectedIndex > -1 Then
sDiv = ddlDivision.SelectedValue
End If

If ddlGroup.SelectedIndex > -1 Then
sGrp = ddlGroup2.SelectedValue
End If

If ddlResource.SelectedIndex > -1 Then
sRes = ddlResource.SelectedValue
End If

Dim sqlGrp As String = "SELECT GC+' - '+GD as Expr2, GC, GD, "
sqlGrp &= "DC FROM [RL-DivG]"

If Not sDiv = Nothing Then
sqlGrp = sqlGrp & " WHERE DC ='" & sDiv & "' "
End If
If Not sGrp = Nothing Then
sqlGrp = sqlGrp & " and GD ='" & sGrp & "' "
End If

Dim cmdGrp As New OleDbCommand(sqlGrp, Con1)
Con1.Open()
Dim myRDR As OleDbDataReader = cmdGrp.ExecuteReader()
If myRDR.HasRows = True Then
ddlGroup2.DataSource = myRDR
ddlGroup2.DataTextField = "Expr2"
ddlGroup2.DataValueField = "GD"
ddlGroup2.DataBind()
End If
Con1.Close()

SelectListBoxValue(ddlDivision, sDiv)
SelectListBoxValue(ddlGroup2, sGrp)
SelectListBoxValue(ddlResource, sRes)

lblDiv.text = GetListBoxValue(ddlDivision, "Unknown")
lblGrp2.text = GetListBoxValue(ddlGroup2, "Unknown")
lblRes.text = GetListBoxValue(ddlResource, "Unknown")

End Sub

Private Sub SelectListBoxValue(ByRef MylistBox As Object, ByVal MyVal As Object)
Dim obj As DropDownList = CType(MylistBox, DropDownList)
Dim v As String = CStr(MyVal)

If Not v = Nothing Then v = v.ToLower
Dim i As Integer
For i = 0 To obj.Items.Count - 1
If CType(obj.Items(i), ListItem).Value.ToLower = v Then
obj.SelectedIndex = i
Exit For
End If
Next
End Sub

Private Function GetListBoxValue(ByVal obj As Object, Optional ByVal sDefaultValue As Object = "") As Object
If obj.SelectedIndex = -1 Then Return sDefaultValue
'Return obj.SelectedItem.Value
Return obj.items(obj.SelectedIndex).value
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top