473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_SelectedIndexChanged

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="true" 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
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.         'Read Sports
  4.         Dim strSportSQL As String
  5.         strSportSQL = "SELECT sportID,sportName FROM TblSport ORDER BY sportName ASC "
  6.  
  7.         Dim DataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/Database.mdb"))
  8.  
  9.         Dim SportCommand As New OleDbCommand(strSportSQL, DataConn)
  10.  
  11.         DataConn.Open()
  12.  
  13.         Dim SportDBReader As OleDbDataReader = SportCommand.ExecuteReader()
  14.         DDLSports.DataSource = SportDBReader
  15.         DDLSports.DataTextField = "sportName"
  16.         DDLSports.DataValueField = "sportID"
  17.         DDLSports.DataBind()
  18.         SportDBReader.Close()
  19.  
  20.         DataConn.Close()
  21.  
  22.     End Sub
  23.  
  24.  
  25. Protected Sub DDLSports_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLSports.SelectedIndexChanged
  26.  
  27.  
  28.         'Read Leagues
  29.         Dim strLeagueSQL As String
  30.         strLeagueSQL = "SELECT leagueID,leagueName FROM TblLeague WHERE sportID = " & DDLSports.SelectedValue & " ORDER BY leagueName ASC"
  31.  
  32.         Dim DataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/Database.mdb"))
  33.  
  34.         Dim LeagueCommand As New OleDbCommand(strLeagueSQL, DataConn)
  35.         DataConn.Open()
  36.  
  37.         Dim LeagueDBReader As OleDbDataReader = LeagueCommand.ExecuteReader()
  38.  
  39.         DDLLeagues.DataSource = LeagueDBReader
  40.         DDLLeagues.DataTextField = "leagueName"
  41.         DDLLeagues.DataValueField = "leagueID"
  42.         DDLLeagues.DataBind()
  43.         LeagueDBReader.Close()
  44.  
  45.     End Sub
  46. End Class
  47.  
Thanks Ash
Mar 29 '08 #1
5 1027
balabaster
797 Expert 512MB
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:
http://www.asp.net/AJAX/AjaxControlT...gDropDown.aspx

This will allow you to do a background postback so your page won't need to reload on selection from the first dropdownlist.
Mar 30 '08 #2
Ahh great, thank you.
however which dropdown list should I set EnableViewState to true on?
also what is that actually doing?

Ash
Mar 30 '08 #3
balabaster
797 Expert 512MB
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.
Mar 30 '08 #4
Thank you, its finally working perfectly.
Mar 30 '08 #5
balabaster
797 Expert 512MB
Thank you, its finally working perfectly.
No problem, glad I could help
Mar 30 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shane O. Pinnell | last post by:
I am sure this has come up before, but I haven't been able to find an answer as of yet. That said, any help is definitely appreciated! I have a datagrid populated from a dataset. I have a...
1
by: Mark | last post by:
I'm using dropdownlist in a datagrid to allow a column in a table to be edited. The dropdownlist contains several the from "dictionary/ code-decode" table. These columns are NOT required in the...
3
by: Tim::.. | last post by:
Can someone tell me how I get the correct selected office in my drop down list when I enter edit mode. At the moment it just defaults to the office at the top of the drop down list rather than...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
1
by: Michael Kolias | last post by:
Hi everybody, I am having a problem getting the selected value of a drop down list that is populated dynamically inside a datagrid control. When I try to access the selected item on the...
1
by: Frank | last post by:
Hello, I have some text boxes and 2 drop down lists (DDL) within a panel like so: Everything updates fine but the values from the DDLs are always the items selected by default. In other words,...
4
by: ^MisterJingo^ | last post by:
Hi all, I have a web page which has a single dropdownlist containing 3 items. Below the dropdownlist are two listboxes. Depending on the option selected from the dropdownlist, the left most...
3
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr...
0
by: ana_gil | last post by:
hello i have a problem with a dropdown list i am populating from an Access table. I´ve seen this problem in other discussions but i can´t seem to find the solution. please help. my .aspx has...
2
by: needin4mation | last post by:
I remember in 1.1 that I would put not to populate a given dropdownlist unless it was not Page.IsPostBack. But in 2.0 where I use the DAL and no codefile, I don't see where to effect this. Any...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.