AJAX Combo box
my problem is that i have two combo box in asp.net .
one combo box has keep information about country .
second combo box has keep information about city .
both combo box has populated from database .
when i select any country from country combo box the second combo box (city
) has populatd from database without refreshing the page through AJAX.
please give the solution .
suggested ajax solution
Here's my best guess at a solution.
1. Include the following general-purpose script. (It could be maintained as a separate file and included as follows:
var _req // XmlHttpRequest
function sendReq(page, qry, handler)
_req = new XMLHttpRequest()
else if (window.ActiveXObject)
_req = new ActiveXObject("Microsoft.XMLHTTP")
alert ("Browser does not support HTTP Request.")
_req.onReadyStateChange = handler
var url = page + "?q=" + str
_req.open("GET", url, true)
2. In the combo box for Country, include the following:
In the city combo box, include the following:
<option>Please select a country first.</option>
sendReq('myPage.aspx', country, 'cityListReturned')
4. In your server page, include capability to look up the list of cities based on the Country returned in the query string, e.g. ?q=UK. The returned text should be in the html format to fill the city combo box, e.g.
4a. Since the list of cities should be relatively static, you might want to save trips to the database by maintaining a self-populating object in the Application:
Private Shared _cityList As DataTable
Public Shared Function CityList(ByVal country As String) As String
If _cityList Is Nothing Then
Dim ds As DataSet = QueryForCompleteListOfCitiesAndCountries()
_cityList = ds.Tables(0)
Dim htmlList As String = ""
Dim dv As New DataView(_cityList)
dv.Filter = "country='" & country & "'"
dv.Sort = "city"
For c = 0 To dv.Items.Count - 1
htmlList &= "<option>" & dv.Items(c)("city").ToString & "</option>" & vbCrLf
if (_req.readyState == 4 || _req.readyState == "complete")
document.getElementById("city").innerHTML = _req.responseText
(Caution: I've never done this -- just having fun trying to guess how to solve it.)
Last edited by kitwest; 12-27-2006 at 12:41 PM.
Reason: improve readability
By viperor in forum Database
Last Post: 04-09-2008, 08:28 AM
By jherndon in forum .NET
Last Post: 12-23-2005, 12:05 PM
By Larry Muller in forum VB Classic
Last Post: 02-24-2001, 11:12 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL