Passing Values into ASP Pages
by David Berry
Sep 4th, 1999
You might have come across this situation; Let's say you had a
catalog page that listed all the products you had to sell on your site. After displaying
the page (by retrieving the records from your database using ASP) you wanted the customer
to be able to click on a particular product and go to another page to see the details for
that product. The purpose of this tutorial is to describe how you pass parameters and
values to another ASP page to retrieve details about the item. Just follow the steps
below.
Step 1: Create a hyperlink that passes a parameter.
You've probably seen URLs in your browser that have are extremely
long with the name of the page and then a ? symbol with a bunch of information after it,
such as http://www.xyz.com/register.asp?Name=David. The question mark indicates that
you're passing a parameter within the URL. First let's create a hyperlink to pass values
to a details page.
<a href="details.asp?Details=Computers">Show Details</a>
<a href="details.asp?Details=<% = RS("ProductType")
%>"><% = RS("ProductType") %></a>
In the code above, Details is the name of the QueryString we're
going to pass and "Computers" is the value we want to retrieve.
Details=Computers is a key-value pair.
Step 2: Retrieving the Values
Once you've created your hyperlink, you can access this
information by using the Request object's QueryString
collection. In the details.asp page, you can access the value of the keys by using
their names with the Request object. In this example we're using a System DSN called
Catalog and a table called Products.
<%
Dim Connect ,RS, Query, Details
Details = Trim(Request("Details"))
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "Catalog"
Set RS = Server.CreateObject("ADODB.Recordset")
Query = "SELECT * FROM Products "
Query = Query & "WHERE ProductType='"& Details & "'
"
Query = Query & "ORDER BY [ProductName]"
RS.Open Query, Connect
%>
Step 3: Checking for empty results
It's always a good idea to check to make sure the the query
returned records, if not display an informational message.
<%
If RS.EOF Then
Response.Write "There are no Details available for " &
Request("Details")"
Else
End If
%>
Step 4: Displaying our Details
The code for details.asp is as shown below. You can modify it
accordingly.
<%@Language=VBScript %>
<HTML>
<BODY>
<%
Dim Connect ,RS, Query, Details
Details = Trim(Request("Details"))
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "Catalog"
Set RS = Server.CreateObject("ADODB.Recordset")
Query = "SELECT * FROM Products "
Query = Query & "WHERE ProductType='"& Details & "'
"
Query = Query & "ORDER BY [ProductName]"
Set RS = Conn.Execute(Query)
If RS.EOF Then
Response.Write "There are no Details available for " &
Request("Details")"
Else
%>
<table border="1" width="100%" cellpadding="2">
<tr>
<td><strong><font face="Verdana"
size="2">Name</font></strong></td>
<td><strong><font face="Verdana" size="2">Product
Type</font></strong></td>
<td><strong><font face="Verdana"
size="2">Details</font></strong></td>
<td><strong><font face="Verdana"
size="2">Price</font></strong></td>
</tr>
<%
Do While Not RS.EOF
%>
<tr>
<td><font face="Verdana" size="2"><% =
RS("Name") %></font></td>
<td><font face="Verdana" size="2"><% =
RS("ProductType") %></font></td>
<td><font face="Verdana" size="2"><% =
RS("Details") %></font></td>
<td><font face="Verdana" size="2"><% =
RS("Price") %></font></td>
</tr>
<%
RS.MoveNext
Loop
%>
</table>
<%
End If
RS.Close
Set RS = Nothing
Conn.Close
Set Conn = Nothing
%>
</BODY>
</HTML>
Using the above method you have seen how to pass a value to an
ASP page using a QueryString.
About the Author
David Berry is a Microsoft MVP for FrontPage. He runs a FrontPage support site at http://www.net-sites.com/sitebuilder

|