Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 11th, 2007, 09:02 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 121
Master / Detail pages

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
  1. <%
  2. ConnDB = Server.MapPath("data\Master_Detail.mdb")
  3.  
  4. Set Conn = Server.CreateObject("ADODB.Connection")
  5. Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
  6. Conn.Open ConnDB
  7.  
  8. Set rsListAll = Server.CreateObject("ADODB.Recordset")
  9. rsListAllSQL = "SELECT item_id, item_title FROM Items"
  10.  
  11. rsListAll.Open rsListAllSQL,Conn
  12. %>
  13. <h1>List of Items</h1>
  14. <p>
  15. <%
  16. While Not rsListAll.EOF
  17.   %>
  18.   <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>
  19.   <%
  20.   rsListAll.MoveNext
  21. Wend
  22. %>
  23. </p> 
  24. <%
  25. rsListAll.Close
  26. Set rsListAll = Nothing
  27.  
  28. Conn.Close
  29. Set Conn = Nothing
  30. %>



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
  1. <%
  2. item_id = Request.QueryString("item_id")
  3.  
  4. If Len(Trim(item_id)) = 0 Or Not IsNumeric(item_id) Then
  5.     errMsg = "Invalid item_id"
  6.     PageTitle = "Error: " & errMsg
  7. Else
  8.     ConnDB = Server.MapPath("data\Master_Detail.mdb")
  9.    
  10.     Set Conn = Server.CreateObject("ADODB.Connection")
  11.     Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
  12.     Conn.Open ConnDB
  13.    
  14.     Set rsListItem = Server.CreateObject("ADODB.recordset")
  15.     rsListItemSQL = "SELECT item_id, item_title, item_description, item_image FROM Items WHERE item_id = " & item_id
  16.     rsListItem.Open rsListItemSQL, Conn
  17.    
  18.     If Not rsListItem.EOF Then
  19.         item_id = rsListItem("item_id")
  20.         item_title = rsListItem("item_title")
  21.         item_description = rsListItem("item_description")
  22.         item_image = rsListItem("item_image")
  23.         PageTitle = "Item Detail: " & item_title
  24.     Else
  25.         errMsg = "item_id not found"
  26.         PageTitle = "Error: " & errMsg
  27.     End If
  28.    
  29.     rsListItem.Close
  30.     Set rsListItem = Nothing
  31.    
  32.     Conn.Close
  33.     Set Conn = Nothing
  34. End If
  35. %>


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
__________________
CyberTechHelp

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Master / Detail pages


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway