2 dropdown list,second one only show options corresponding to the 1st

Discussion in 'C#' started by wagae, Apr 2, 2007.

  1. #1
    attached is the code for the 2 dropdown list of which their values are retrieved from the database,how can i make the 2nd dropdown values correspond to what was selected in the first dropdown?

    For example,if the 1st dropdown list is CarMakes, and the second one is CarModels,when a CarMake is selected,show appropriate CarModels.

    'This is how i get values into my first dropdown list

    Private Sub MyEts()
    Dim oConn As SqlConnection
    Dim oCMD As SqlCommand
    Dim oDR As SqlDataReader
    Dim oLI As System.Web.UI.WebControls.ListItem

    ddl_Et.Items.Clear()
    oConn = New SqlConnection(ConfigUtility.CONNN)
    oCMD = New SqlCommand("Execute ReturnEts")
    oCMD.Connection = oConn
    oConn.Open()
    oDR = oCMD.ExecuteReader
    If oDR.HasRows Then

    While oDR.Read
    oLI = New System.Web.UI.WebControls.ListItem()
    oLI.Value = CStr(oDR("ID")).Trim
    oLI.Text = CStr(oDR("DescriptionID")).Trim
    ddl_Et.Items.Add(oLI)
    End While
    Else
    'nothing
    End If
    oDR.Close()
    oConn.Close()

    End Sub

    'ASP part

    <aspropDownList ID="ddl_Et" runat="server" AutoPostBack="True"></aspropDownList>


    'then my second dropdown,values link by ID to the first DDL table

    Private Sub EventOptions(ByVal EventID As Integer)
    Dim oConn As SqlConnection
    Dim oCMD As SqlCommand
    Dim oDR As SqlDataReader
    Dim oLI As System.Web.UI.WebControls.ListItem

    ddl_EOptions.Items.Clear()
    oConn = New SqlConnection(ConfigUtility.CONNN)
    oCMD = New SqlCommand("Execute RetOptions '" & EID & "'")
    oCMD.Connection = oConn
    oConn.Open()
    oDR = oCMD.ExecuteReader
    If oDR.HasRows Then

    While oDR.Read
    oLI = New System.Web.UI.WebControls.ListItem()
    oLI.Value = CStr(oDR("sOption")).Trim
    oLI.Text = CStr(oDR("sOption")).Trim
    ddl_EOptions.Items.Add(oLI)
    End While
    Else
    'nothing
    End If
    oDR.Close()
    oConn.Close()
    End Sub


    <aspropDownList ID="ddl_EOptions" runat="server" AutoPostBack="True"></aspropDownList>
     
    wagae, Apr 2, 2007 IP
  2. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    druidelder, Apr 3, 2007 IP
  3. Adi

    Adi Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    1. If there aren't many values in the 2nd combo box you can write all the combo boxes to the HTML page with display= none . When a user will select a value in the first combo box it will trigger the onChange event which will display the needed combo-box. (Make sure that your page size stays small -If not you need to use other solution)
    2. I strongly recommend to save the query result in the Application object - you relay don't want to access the database each time a user browse your site.
     
    Adi, Apr 3, 2007 IP
  4. briansol

    briansol Well-Known Member

    Messages:
    221
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #4
    this is a perfect example of where ajax is the best choice.
     
    briansol, Apr 11, 2007 IP
  5. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #5
    ludwig, Apr 11, 2007 IP