Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

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

Edited by PlausiblyDamp
  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

Suggest that you place the code that fills the second list box in a seperate method that takes a sting as an input variable:

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

if not page.ispostback then
end if

 

and it should work.

 

Mike55

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...