Drop Down List-ASP.NET

zoey1200

Newcomer
Joined
Apr 3, 2004
Messages
3
I have a drop down list that get the values from the database allows the user to select a value and then based on that value populates another drop down list with other values from the database.

My problem is when the first item in the lists is selected it will not populate the next drop down list. How do I fix this.

Here is my code:
Visual Basic:
 On Error GoTo Error_Handler

        Dim pRS As New ADODB.Recordset
        Dim psTemp As String
        Dim psSQL As String = "SELECT PROG_ID, PROG_DESCR "
        Dim psFrom As String = "FROM PROGRAM_VW"
        Dim psWhere As String = vbNullString
        Dim psOrder As String = "ORDER BY PROG_DESCR"

        Dim piIndex As Integer = 0

        Dim pbItemSelected As Boolean = False

        psTemp = psSQL & Chr(32) & psFrom & Chr(32) & psWhere & Chr(32) & psOrder

        '** Open the Recordset using the HMS database **'
        pRS = eJDS.Global.GetOracleRecordset("JDS", psTemp, Session)

        If pRS Is Nothing Then
            '** Indicate no records found **'
            lblHeader.Text = "No Items to Select - Select Cancel or Back"
            cboProgram.Items.Add(New ListItem("No Items to Select - Select Cancel or Back"))
            '            btnSubmit.Visible = False
            GoTo Sub_Exit
        End If

        With pRS
            If .RecordCount <> 0 Then
                '** Clear the list box **'
                cboProgram.Items.Clear()

                '** Move to the first record in the recordset **'
                .MoveFirst()

                '** Loop through records until the end of the file is reached **'
                Do While Not .EOF
                    '** Add the report name **'
                    cboProgram.Items.Add(New ListItem(.Fields("PROG_DESCR").Value, .Fields("PROG_ID").Value))
                    .MoveNext()
                    '** Increment the counter **'
                    piIndex = piIndex + 1
                Loop

                '** Close the recordset **'
                pRS.Close()

                '** Show the list box **'
                cboProgram.Visible = True
            Else
                '** Indicate no records found **'
                NoRecs()
                GoTo Sub_Exit
            End If
        End With

        '** Do this the first time the page is loaded only **'
        If Not IsPostBack Then
            '** Select the item in the list, if it has previously been selected **'
            If cboProgram.Items.Count > 0 And (Not pbItemSelected) Then
                '** Select the first item in the list be default **'
                cboProgram.Items(0).Selected = True

            End If

        End If

Sub_Exit:
        pRS = Nothing
        Exit Sub

Error_Handler:
        '** Set the global error message variable **'
        Session("ErrorMessage") = "" & _
                "The following error occured during " & _
                Session("msModule") & " LoadProgramList Event." & _
                vbCrLf & vbCrLf & _
                Err.Number & vbTab & Err.Description
        Response.Redirect("frmError.aspx")
        Err.Clear()
        GoTo Sub_Exit
    End Sub
 
Last edited by a moderator:
What event is this code in? If you are doing this in a Page_Load event you will want to check if the page is being generated as part of a post back (Page.IsPostBack) and only build the first listbox on the initial page load.

Also you may find moving from ADO to ADO.Net and using the ASP.Net controls data binding facilities could make this a lot easier (either from a dataset or possibly a data reader).
 
I have it on the PageLoad with AutoPostBack. The only listbox on PageLoad is this one.

The problem is that when you select the very first item in any of the listboxes it is supposed to select that item and prepopulate the next listbox based on that value. Right now it will not do that. If I select any of the other items in the listboxes it will pre-populate the following listbox just fine. Also if I click on any item besides the first item and then go back and click on the first item it pre-populates fine.
 
Do you have code in the listboxes SelectedItemChanged event that needs to be executed when the selection changes? If so either call the event manually on the page load or don't set any item to be the default and require the user to make a choice.
 
Suggest that you place the code that fills the second list box in a seperate method that takes a sting as an input variable:
Code:
method name ( byval x as string)
end method

The when you make a selection from the first list, you call that method name and pass to it the value that you selected.

Now for the first value, take the method name and put it between the lines
Code:
if not page.ispostback then
end if

and it should work.

Mike55
 
Back
Top