Easiest way to do this is with the three combo boxes as suggested below. I do
that exact same thing with one of my programs, The country CBO is filled from
a database of countries. When you select a country from the combo, it then
fills the state/region combo with the associated data from the database.
Selecting a state/region will fill the city combo with the associated data.
Selecting the city will pull the associated data -- in my case it is latitude,
longitude, and other geographical data.
here is how I do it with my databases.
Country
Private Sub cboMiscCountry_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cboMiscCountry.SelectedIndexChanged
' Select a country and get a lit of state/regions
' if there are no matching state/regions, then disable the state/region combo
' and fill the city combo with assofcaited data
Dim country As String = cboMiscCountry.SelectedItem
Dim sql As String
Dim dt As New DataTable
Me.Cursor = Cursors.WaitCursor
cboMiscState.Items.Clear()
cboMiscState.Text = ""
cboMiscCity.Items.Clear()
cboMiscCity.Text = ""
Application.DoEvents()
' get the country code
RegionCode = ""
sql = "select region from iso3166 where `name`='" & country & "'"
dt = ExeccuteMysqlQuery(sql)
CountryCode = dt.Rows(0).Item("region")
'get the regions
If CountryCode = "US" Then
sql = "select name from iso3166_2 where `country`='" & CountryCode
& "'"
Else
sql = "select name from fips_104 where `country`='" & CountryCode &
"'"
End If
dt = ExeccuteMysqlQuery(sql)
If dt.Rows.Count > 0 Then
cboMiscState.Enabled = True
For Each row As DataRow In dt.Rows
cboMiscState.Items.Add(row.Item("name"))
Next
Else
cboMiscState.Enabled = False
sql = "select city from locations where `country`='" & CountryCode
& "'"
dt = ExeccuteMysqlQuery(sql)
If dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
cboMiscCity.Items.Add(row.Item("city"))
Next
End If
End If
Me.Cursor = Cursors.Default
End Sub
State/Region
Private Sub cboMiscState_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cboMiscState.SelectedIndexChanged
'get a list of cities that match the state/region
Dim state = cboMiscState.SelectedItem.ToString.Replace("'", "\'")
cboMiscCity.Items.Clear()
cboMiscCity.Text = ""
Dim sql As String
Label139.Visible = True
cboMiscCity.Visible = True
Me.Cursor = Cursors.WaitCursor
Application.DoEvents()
Dim dt As New DataTable
If CountryCode = "US" Then
sql = "select region from iso3166_2 where `name`='" & state & "'"
Else
sql = "select region from fips_104 where `name`='" & state & "'"
End If
dt = ExeccuteMysqlQuery(sql)
RegionCode = dt.Rows(0).Item("region").ToString.Replace("'", "\'")
sql = "select city from locations where `country`='" & CountryCode & "'
and `region`='" & RegionCode & "'"
dt = ExeccuteMysqlQuery(sql)
If dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
cboMiscCity.Items.Add(row.Item("city"))
Next
Else
sql = "select city from locations where `country`='" & CountryCode & "'"
dt = ExeccuteMysqlQuery(sql)
For Each row As DataRow In dt.Rows
cboMiscCity.Items.Add(row.Item("city"))
Next
End If
Me.Cursor = Cursors.Default
End Sub
City
Private Sub cboMiscCity_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cboMiscCity.SelectedIndexChanged
Dim mygooglemap As String = googlemap
Dim city As String = cboMiscCity.SelectedItem.ToString.Replace("'", "\'")
Dim dtSunrise, dtSunset, dtSolarnoon As Date
Dim sdtSunrise, sdtSunset, sdtSolarnoon As String
Dim lat, lon As Double
Dim dt As New DataTable
Dim bearingto, distanceto, bearingfr As Integer
Dim sql, str, grid, latdms, londms As String
If RegionCode <> "" Then
sql = "select * from locations where `country`='" & CountryCode &
"' and `region`='" & RegionCode & "' and `city`='" & city & "'"
Else
sql = "select * from locations where `country`='" & CountryCode &
"' and `city`='" & city & "'"
End If
dt = ExeccuteMysqlQuery(sql)
If dt.Rows.Count > 0 Then
'do your city infor from here where
End If
End Sub
Jeff K. Steinkamp N7YG
Tucson, AZ
SCUD Missile Coordinates:
N032-13-55.02 W110-55-52.79
Registered Linux User: 420428
------------------------------------------------------
WBT: Water Binary Tree
From: Sammael
Sent: Tuesday, September 21, 2010 11:03 AM
To: [email protected]
Subject: Re: [DotNetDevelopment] Re: Populating Countries, States and City in a
Cascading dropdownlist in asp.net with or without AJAX
probably him wanna do something like this:
countryComboBox
stateComboBox
cityComboBox
when he choose the Country, the stateComboBox shows all Stats in this country.
same when he chooses the state, all cities appears in cityComboBox
2010/9/21 Greg Hile <[email protected]>
Do you mean you have several Dropdown boxes and would like each one to
populate based upon the previous one's selection?
If that is the case then you can do it a ton of ways.
Do you want to use a database (or even have access to one), a List<String>,
an array , etc?
Are you using .Net 3.x or above (LINQ can be used).
Give us more information as to what you are trying to accomplish and what
resources you have to get the job done and I am sure you will get a speedy
reply.
Thurston Howell III
That would probably make more sense than having one dropdown list with
everything in it. (That would be simple to accomplish. Time consuming, not
fun, but simple).
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Cerebrus
Sent: Tuesday, September 21, 2010 3:06 AM
To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting
Subject: [DotNetDevelopment] Re: Populating Countries, States and City in a
Cascading dropdownlist in asp.net with or without AJAX
That's it ?
On Sep 20, 10:38 pm, Thamizh Jain <[email protected]> wrote:
> I need complete source code for populating all the countries, states
> and cities information in a Cascading drop downlist with or without
> using AJAX control toolkit.
>
> Thanks
> Thamizh
--
Bayushi Sanjiro (バユシ サンジロ)
Scorpion Clan * Loyal * Unique * Samurai * Poison Master * Courtier * Imperial
Historian
♠ Your secrets are safe with me...