Here is an example of how to list all records on one page then have a link to show the individual record on another page.
Example of database
Code:
item_id item_title item_description item_image
1 Blue Square This is an image of a Blue Square BlueSquare.gif
2 Blue Circle This is an image of a Blue Circle BlueCircle.gif
3 Pink Square This is an image of a Pink Square PinkSquare.gif
4 Pink Circle What does it look like? PinkCircle.gif
Master.asp
The master page has a simple piece of code to select all items from the table then loop through them
asp Code:
Original
- asp Code |
|
|
|
<%
ConnDB = Server.MapPath("data\Master_Detail.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
Conn.Open ConnDB
Set rsListAll = Server.CreateObject("ADODB.Recordset")
rsListAllSQL = "SELECT item_id, item_title FROM Items"
rsListAll.Open rsListAllSQL,Conn
%>
<h1>List of Items</h1>
<p>
<%
While Not rsListAll.EOF
%>
<a href="Detail.asp?item_id=<%=rsListAll("item_id")%>" title="Click here to see the item details for <%=rsListAll("item_title")%>"><%=rsListAll("item_title")%></a><br>
<%
rsListAll.MoveNext
Wend
%>
</p>
<%
rsListAll.Close
Set rsListAll = Nothing
Conn.Close
Set Conn = Nothing
%>
The part of the code that will enable the detail page to display the correct record is the hyperlink
The hyperlink will pass the record id (item_id) in the querystring
ASP HTML Code:
Original
- ASP HTML Code |
|
|
|
<a href="Detail.asp?item_id=<%=rsListAll("item_id")%>" title="Click here to see the item details for <%=rsListAll("item_title")%>"><%=rsListAll("item_title")%></a>
HTML Code:
Original
- HTML Code |
|
|
|
<a href="Detail.asp?item_id=1" title="Click here to see the item details for Blue Square">Blue Square</a>
Detail.asp
The detail script will
Request the querystring variable, do a little bit of validation, then pass the variable to the SQL Query
Request QueryString Code:
Original
- Request QueryString Code |
|
|
|
item_id = Request.QueryString("item_id")
Validation Code:
Original
- Validation Code |
|
|
|
If Len(Trim(item_id)) = 0 Or Not IsNumeric(item_id) Then
errMsg = "Invalid item_id"
PageTitle = "Error: " & errMsg
SQL Query Code:
Original
- SQL Query Code |
|
|
|
rsListItemSQL = "SELECT item_id, item_title, item_description, item_image FROM Items WHERE item_id = " & item_id
If the query is valid and finds a record then you can output the item details
Otherwise you can output an appropriate error message
asp Code:
Original
- asp Code |
|
|
|
<%
item_id = Request.QueryString("item_id")
If Len(Trim(item_id)) = 0 Or Not IsNumeric(item_id) Then
errMsg = "Invalid item_id"
PageTitle = "Error: " & errMsg
Else
ConnDB = Server.MapPath("data\Master_Detail.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
Conn.Open ConnDB
Set rsListItem = Server.CreateObject("ADODB.recordset")
rsListItemSQL = "SELECT item_id, item_title, item_description, item_image FROM Items WHERE item_id = " & item_id
rsListItem.Open rsListItemSQL, Conn
If Not rsListItem.EOF Then
item_id = rsListItem("item_id")
item_title = rsListItem("item_title")
item_description = rsListItem("item_description")
item_image = rsListItem("item_image")
PageTitle = "Item Detail: " & item_title
Else
errMsg = "item_id not found"
PageTitle = "Error: " & errMsg
End If
rsListItem.Close
Set rsListItem = Nothing
Conn.Close
Set Conn = Nothing
End If
%>
ASP HTML Code:
Original
- ASP HTML Code |
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><%=PageTitle%></title>
</head>
<body>
<h1>Item Detail </h1>
<% If errMsg <> "" Then %>
<p><strong>Error:</strong> <%=errMsg%></p>
<% Else %>
<table width="200" border="1" cellspacing="0" cellpadding="5">
<tr>
<td>item_id</td>
<td><%=item_id%></td>
</tr>
<tr>
<td>item_title</td>
<td><%=item_title%></td>
</tr>
<tr>
<td>item_description</td>
<td><%=item_description%></td>
</tr>
<tr>
<td>item_image</td>
<td><%=item_image%></td>
</tr>
<tr>
<td colspan="2"><div align="center"><img src="Images/<%=item_image%>" alt="<%=item_title%>"></div></td>
</tr>
</table>
<% End If %>
<p>Back to <a href="Master.asp">List of Items</a> </p>
</body>
</html>
You can find example files here
http://computer-helpforum.com/asp/d...ster_Detail.zip
Here is an example of the scripts
Master.asp
Here is an example of the error checking
Detail.asp?item_id=abc
Detail.asp?item_id=123