A DropDownList that populates another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ashlewis
    New Member
    • Mar 2008
    • 20

    A DropDownList that populates another

    Before you kill me i know theres a million different tutorials out there, but ive been stuck all day on this. (im using VB)

    Basically I have a sport list and a league list.
    The sports list is populated on_load from a database.
    When i select an item from the sports list i want it to populate the league list with the corresponding leagues.

    The solution ive attempted is to populate the leagues in the DDLSports_Selec tedIndexChanged

    The items to go into the league list is also from the database and the query to select it has the currently selected value of the sports menu (i assume this is the way to do it)

    However, the problem arises since obviously i have to make the sports list AutoPostBack="t rue" since there needs to be a server call to retrive the data.

    When the page reloads, the form is reset and the Sports field is selecting the top item again not the new one, and hence the league list is not displaying what is needed.

    I'll Stick in the VB code if that helps
    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            'Read Sports
            Dim strSportSQL As String
            strSportSQL = "SELECT sportID,sportName FROM TblSport ORDER BY sportName ASC "
    
            Dim DataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/Database.mdb"))
    
            Dim SportCommand As New OleDbCommand(strSportSQL, DataConn)
    
            DataConn.Open()
    
            Dim SportDBReader As OleDbDataReader = SportCommand.ExecuteReader()
            DDLSports.DataSource = SportDBReader
            DDLSports.DataTextField = "sportName"
            DDLSports.DataValueField = "sportID"
            DDLSports.DataBind()
            SportDBReader.Close()
    
            DataConn.Close()
    
        End Sub
    
    
    Protected Sub DDLSports_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLSports.SelectedIndexChanged
    
    
            'Read Leagues
            Dim strLeagueSQL As String
            strLeagueSQL = "SELECT leagueID,leagueName FROM TblLeague WHERE sportID = " & DDLSports.SelectedValue & " ORDER BY leagueName ASC"
    
            Dim DataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/Database.mdb"))
    
            Dim LeagueCommand As New OleDbCommand(strLeagueSQL, DataConn)
            DataConn.Open()
    
            Dim LeagueDBReader As OleDbDataReader = LeagueCommand.ExecuteReader()
    
            DDLLeagues.DataSource = LeagueDBReader
            DDLLeagues.DataTextField = "leagueName"
            DDLLeagues.DataValueField = "leagueID"
            DDLLeagues.DataBind()
            LeagueDBReader.Close()
    
        End Sub
    End Class
    Thanks Ash
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    In the Page_Load, wrap your code with:


    If Not Page.IsPostBack Then

    'Do your stuff

    End If


    On your dropdownlist set the EnableViewState property to true.

    That should fix your problem.

    Something else that may be of help:
    Build web apps and services that run on Windows, Linux, and macOS using C#, HTML, CSS, and JavaScript. Get started for free on Windows, Linux, or macOS.


    This will allow you to do a background postback so your page won't need to reload on selection from the first dropdownlist.

    Comment

    • Ashlewis
      New Member
      • Mar 2008
      • 20

      #3
      Ahh great, thank you.
      however which dropdown list should I set EnableViewState to true on?
      also what is that actually doing?

      Ash

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by Ashlewis
        Ahh great, thank you.
        however which dropdown list should I set EnableViewState to true on?
        also what is that actually doing?

        Ash
        The EnableViewState means that it will hold its currently selected values between page loads (unless the code in your page_load method changes that). You should set it on the first drop down...and your second if you're going to load the data for the selected league into the same page too.

        Comment

        • Ashlewis
          New Member
          • Mar 2008
          • 20

          #5
          Thank you, its finally working perfectly.

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by Ashlewis
            Thank you, its finally working perfectly.
            No problem, glad I could help

            Comment

            Working...