Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SYNCHRONIZED LIST BOXES 5

Status
Not open for further replies.

fspeckman

Programmer
Dec 28, 2000
3
US
I want to have 2 list boxes on a web page. The first box will have the names of the states. When a user selects a state in the first list box I want to populate the second list box with the 10 biggest cities in that state without going back to the server. This would be easy using JavaScript and the DOM but I want to use VBScript. Anyone know how to do this?
 
Did anyone actually provide the answer for this ? I would like to know also...

Cheers
 
you could use rds to achieve this with vbscript.
-1 hit on the server
-dynamic 2nd listbox

basically you'd have 2 instances of the rds control class set to hidden / 1 for states and and the other for cities. the underlying sql statement for the 2nd control would have a state variable which would determine what cities to retrieve (this would be trapped from clicking the 1rst combobox).

the State combobox is based on the 1rst rds control, the City combobox is based on the second.
 
Here's how I finally did it. NO hits to the server. Works OK as long as the total number of cities isn't very large. All the necessary data is stored on the page itself. Could be made more efficient by using smaller variable names and delimited strings instead of arrays:

<%
'GET ALL THE STATE AND CITY INFO INTO ARRAYS
Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
OpenString = Session(&quot;OpenString&quot;)
Conn.Open OpenString

Query = &quot;SELECT * FROM State ORDER BY StateName&quot;
Set RS = Conn.Execute(Query)

Dim StateIDs()
Dim StateNames()
i = 0

Do Until RS.EOF
ReDim Preserve StateIDs(i)
StateIDs(i) = RS(&quot;StateID&quot;)
StateNames(i) = RS(&quot;StateName&quot;)
i = i + 1
RS.MoveNext
Loop

NumberOfStates = i - 1

SelectedStateID = StateIDs(0)

Query = &quot;SELECT * FROM City ORDER BY CityName&quot;
Set RS = Conn.Execute(Query)

i = 0
Dim CityIDs()
Dim CityNames()
Dim CityStateIDs() ' STATE ID OF STATE THAT THIS CITY IS IN

Do Until RS.EOF
ReDim Preserve CityIDs(i)
ReDim Preserve CityNames(i)
ReDim Preserve CityStateIDs(i)
CityIDs(i) = RS(&quot;CityID&quot;)
CityNames(i) = RS(&quot;CityName&quot;)
CityStateIDs(i) = RS(&quot;State_ID&quot;)
i = i + 1
RS.MoveNext
Loop

NumberOfCitys = i - 1
%>

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Edit City Name</title>

<%
'CREATE A SCRIPT TO POPULATE THE CITY BOX CORRECTLY WHEN THE STATE BOX CHANGES
Response.Write(&quot;<script LANGUAGE=&quot;&quot;VBScript&quot;&quot;>&quot; & vbCRLF)
Response.Write(&quot;Sub StateName_onChange&quot; & vbCRLF)

Response.Write(&quot;Dim StateIDs(&quot; & NumberOfStates & &quot;)&quot; & vbCRLF)
Response.Write(&quot;Dim StateNames(&quot; & NumberOfStates & &quot;)&quot; & vbCRLF)

For i = 0 To NumberOfStates
Response.Write(&quot;StateIDs(&quot; & i & &quot;) = &quot; & StateIDs(i) & vbCRLF)
Response.Write(&quot;StateNames(&quot; & i & &quot;) = &quot;&quot;&quot; & StateNames(i) & &quot;&quot;&quot;&quot; & vbCRLF)
Next

Response.Write(&quot;Dim CityIDs(&quot; & NumberOfCitys & &quot;)&quot; & vbCRLF)
Response.Write(&quot;Dim CityNames(&quot; & NumberOfCitys & &quot;)&quot; & vbCRLF)
Response.Write(&quot;Dim CityStateIDs(&quot; & NumberOfCitys & &quot;)&quot; & vbCRLF)

For i = 0 To NumberOfCitys
Response.Write(&quot;CityIDs(&quot; & i & &quot;) = &quot; & CityIDs(i) & vbCRLF)
Response.Write(&quot;CityNames(&quot; & i & &quot;) = &quot;&quot;&quot; & CityNames(i) & &quot;&quot;&quot;&quot; & vbCRLF)
Response.Write(&quot;CityStateIDs(&quot; & i & &quot;) = &quot; & CityStateIDs(i) & vbCRLF)
Next

Response.Write(&quot;document.EditCity.City.Length = 0&quot; & vbCRLF)
Response.Write(&quot;SelectedStateID = StateIDs(document.EditCity.StateName.SelectedIndex)&quot; & vbCRLF)
Response.Write(&quot;Dim i&quot; & vbCRLF)
Response.Write(&quot;Dim objElement&quot; & vbCRLF)
Response.Write(&quot;For i = 0 to Ubound(CityStateIDs)&quot; & vbCRLF)
Response.Write(&quot; If SelectedStateID = CityStateIDs(i) Then&quot; & vbCRLF)
Response.Write(&quot; Set objElement = document.createElement(&quot;&quot;OPTION&quot;&quot;)&quot; & vbCRLF)
Response.Write(&quot; ObjElement.text = CityNames(i)&quot; & vbCRLF)
Response.Write(&quot; ObjElement.value = CityNames(i)&quot; & vbCRLF)
Response.Write(&quot; document.EditCity.City.add(objElement)&quot; & vbCRLF)
Response.Write(&quot; End If&quot; & vbCRLF)
Response.Write(&quot;Next&quot; & vbCRLF)
Response.Write(&quot;End Sub&quot; & vbCRLF)
Response.Write(&quot;</script>&quot; & vbCRLF)

%>

</head>

<body>

<table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>

<tr>
<td><font face=&quot;Tahoma&quot; size=&quot;+3&quot; color=&quot;#9999CC&quot;> Edit A City Name</font></td>
</tr>

<form name=&quot;EditCity&quot; action=&quot;edit_city_submit.asp&quot; method=&quot;POST&quot;>

<tr>
<td>
<table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>

<tr>
<td align=&quot;RIGHT&quot;>
<font face=&quot;Arial&quot;> State Name:</font>
</td>
<td>
<select name=&quot;StateName&quot;>
<%
For i = 0 to NumberOfStates
StateName = StateNames(i)
StateID = StateIDs(i)
If SelectedStateID = StateID Then
%>
<option selected value='<%= StateName %>'><%= StateName %></option>&quot;
<% Else%>
<option value='<%= StateName %>'><%= StateName %></option>&quot;
<%
End If
Next
%>
</select>
</td>
</tr>

<tr>
<td align=&quot;RIGHT&quot;>
<font face=&quot;Arial&quot;> City Name:</font>
</td>
<td>
<select name=&quot;City&quot;>
<%
For i = 0 to NumberOfCitys
CityName = CityNames(i)
CityID = CityIDs(i)
CityStateID = CityStateIDs(i)

If SelectedStateID = CityStateID Then
%>
<option value='<%= CityName %>'><%= CityName %></option>&quot;
<%
End If
Next
%>
</select>
</td>
</tr>

<tr>
<td align=&quot;RIGHT&quot;>
<font face=&quot;Arial&quot;> New City Name:</font>
</td>
<td>
<input type=&quot;Text&quot; name=&quot;NewCity&quot; size=&quot;30&quot; maxlength=&quot;30&quot;>
</td>
</tr>
</td>
</tr>

<tr>
<td> </td>
</tr>

<tr>
<td> </td>
<td align=&quot;CENTER&quot;>
<input type=&quot;Submit&quot; value=&quot;Update&quot;>
</td>
</tr>

</table>
</form>
</table>
</body>
</html>

 
Just for anyone else out there, I simplified your script and commented it so that I could get my head around what you had achieved using the document.object manipulation.

Just out of interest does anyone have a help file or something that really explains the Document Object Model and how you can use it to dynamically manpulate stuff? It would be really appreciated as I keep reading stuff about document.object. etc and inner.html and other similar manipulations -> but need to know much more about it !

Here's my edited version for my own understanding:

<html>
<head>
<title>Edit City Name</title>
<%' All this needs to be made server side (not as below)

'CREATE A SCRIPT TO POPULATE THE CITY BOX CORRECTLY WHEN THE STATE BOX CHANGES

<script LANGUAGE=&quot;VBScript&quot;>

Sub StateName_onChange

Dim StateIDs(NumberOfStates)
Dim StateNames(NumberOfStates)

For i = 0 To NumberOfStates
' Dynamically list all the stateids and statenames until end of array
StateIDs(i)=StateIDs(i)
StateNames(i)=StateNames(i)
Next

Dim CityIDs(NumberOfCitys)
Dim CityNames(NumberOfCitys)
Dim CityStateIDs(NumberOfCitys)

For i = 0 To NumberOfCitys

' Dynamically list all the cities, inc. id's names, and the stateid they belong to
CityIDs(i)=CityIDs(i)
CityNames(i)=CityNames(i)
CityStateIDs(i)=CityStateIDs(i)
Next
' empty the city dropdown box of existing data
document.EditCity.City.Length = 0

' Find out what state is selected
SelectedStateID = StateIDs(document.EditCity.StateName.SelectedIndex)

Dim i
Dim objElement
For i = 0 to Ubound(CityStateIDs)
If SelectedStateID = CityStateIDs(i) Then

' Process the following lines if the selectedstateid=citystateid
Set objElement = document.createElement(&quot;OPTION&quot;)

' Creates an <OPTION VALUE=&quot;cityname&quot;>cityname >> for each OPTION NAME

ObjElement.text = CityNames(i)
ObjElement.value = CityNames(i)

' This finally adds the OPTIONs above to the city select box
document.EditCity.City.add(objElement)
End If
Next
End Sub
</script>

' end of server side source
%>

</head>
<body>
 
1. The O'Reilly book: &quot;Dynamic HTML, The Definitive Reference&quot; has the best documentation on the DOM (Document Object Model) that I've found so far. You can preview this book at
2. Regarding your comment &quot;All this needs to be made server side&quot;: The whole point of this exercise for me was to have the City list box change when the user changed the State list box WITHOUT going back to the server. If you don't mind going back to the server every time State changes then there are much easier ways to do this without using the DOM. In the code I provided the City list box changes dynamically on the CLIENT side.
 
Thanks.

Yes your right - I just posted it as it helped me get my head around this particular section, as it was the critical bit. eg. the fact that you can change objects, values, contents, etc on the fly !

Your code led me to Microsoft's MSDN dhtml authoring site which was a good site at least. If anyones interested just goto msdn.microsoft.com and search for 'document object model'. Obvious really !

I'll try the book as well though.

Cheers ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top