日期:2012-12-26  浏览次数:20963 次

The get_list.asp Page
The only remaining part of the task is to create the page get_list.asp, which queries the database to get the matching list of values, and copies these values into the appropriate list box on the main page. The way we do this combines both client-side JavaScript and server-side ASP VBScript into one page.
Probably the simplest and most cross browser compatible way to cache information in a client-side page is through a JavaScript array. We can create this array using server-side ASP code, then read the contents of the array on the client using JavaScript. To see this more clearly, examine the code for the get_list.asp page ?shown and described in the sections below.
Creating a JavaScript Array on the Server
The first part of the page creates a JavaScript SCRIPT section, and within it a function named copyOptions. The first line of this function declares a new array named arrOptions:
<%@LANGUAGE="VBScript"%><HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function copyOptions() {
var arrOptions = new Array;
...
The Server-Side ASP Code
Next comes a section of ASP code that runs on the server. It uses the parameters sent in the query string when the page is loaded to build up a SQL statement like those we discussed earlier on. Then it creates a connection to the pubs database and executes this SQL statement to populate an ADO recordset on the server (remember that you have to change the connection string to specify your SQL Server name):
...
<%
'get the parameters sent in the query string
strFieldName = Request.QueryString("field_name")
strMatchField = Request.QueryString("match_field")
strMatchValue = Request.QueryString("match_value")
'create a connection and a recordset
Set oConn = Server.CreateObject("ADODB.Connection")
Set oRs = Server.CreateObject("ADODB.Recordset")

'** edit the server name and connection details as required **
oConn.Open "SERVER=yourmachine;DATABASE=pubs;DRIVER={SQL Server};UID=sa;PWD=;"
'build up an appropriate SQL statement from the parameter values
strSQL = "SELECT DISTINCT " & strFieldName & " FROM authors"
If Len(strMatchField) Then
   strSQL = strSQL & " WHERE " & strMatchField & "='" & strMatchValue & "'"
End If
strSQL = strSQL & " ORDER BY " & strFieldName

intIndex = 1   'to count the number of values found

'execute the SQL statement and create the JavaScript code lines
Set oRs = oConn.Execute(strSQL)
...
Now comes the clever bit, where we transfer the data from server to client. All we have to do is dynamically create the JavaScript statements that add the data to the array we declared earlier. This JavaScript code will be executed on the client, and so the array and its values will be available there:

...
Do While Not oRs.EOF
  Response.Write "arrOptions[" & intIndex & "] = '" & oRs.Fields(0) & "';" & vbCrlf
  intIndex = intIndex + 1
  oRs.MoveNext
Loop
Set oRs = Nothing
Set oConn = Nothing
%>
...
The Remaining Client-Side JavaScript Code
The rest of the JavaScript code that runs on the client now has to copy the values from the array into the appropriate list box. You'll see that there are a couple of ASP statements embedded in this code to fill in the other values required - such as the number of items in the array and the name of the list box where they are to be placed:
...
// set number of items in array, plus 1 for empty first item
intCount = <% = intIndex %>;   

// get a reference to the list box
var objForm = parent.frames['main'].document.forms['