ASP Lesson 10: Tables and Hyperlinks
This lesson offers yet another example of combining elements. You have seen how easy it is to assign default values to form elements, now you'll see how you can combine a resultset with hyperlinks and querystrings.
Viewing a list of records
It might be convenient to view a list of records in a table format for editing purposes. this lesson will show you how to construct a dynamic table that will include hyperlinked key values to allow you to zoom in on a specific table. When the user clicks on the hyperlink, the student ID is passed via the querystring. We have to modify frmStudent.asp to retrieve the student id, then add a WHERE clause to our SQL string.
Querystring Defined
A querystring is part of a URL, which looks something like this:
frmStudent.asp?sid=00000001
To access the querystring variable from our Script, we use the following method:
localVariable = request.querystring("querystringVariable")
Hyperlink Table Viewer
Source (frmTblView.asp):
<!--#include virtual="/includes/adovbs.inc"-->
<!--#include virtual="/includes/subConn.inc"-->
<%
sql = "SELECT * FROM Students"
openCN
rs.Open sql, cn 'adOpenKeyset specifies the type
'of recordset. Allows full
'navigation
%>
<html>
<head>
<title></title>
</head>
<body>
<form>
<table border="1">
<tr>
<td>CustomerID</td>
<td>Company Name</td>
</tr>
<%do while not rs.EOF%>
<tr>
<td><nobr><a href="frmStudent.asp?sid=<%=rs("sid")%>">
<%=rs("sid")%></a>
</nobr></td>
<td><%=rs("lname") & ", " &
rs("fname")%></td>
</tr>
<%
rs.MoveNext
loop
rs.Close
CloseCN
%>
<tr>
<td colspan="2">
<input type="submit" value="Select Table">
</td>
</tr>
</table>
</form>
</body>
</html>
Sample output:
Student ID | Name |
00000001 | Clown, Bozo |
10164566 | Hung, Jeff |
12312312 | Rogers, Fred |
12654898 | Bodeutsch, Becca |
23492344 | Stone, April |
32742344 | Gates, William |
99425629 | Smith, Brett |
99440419 | Brown, Brent |
Apply what you have learned.
Using the search table you created for this lesson, modify the href argument to link to your grades.asp form to display the grades. Your grades form will need to be modified to request the querystring variable, then use that value in your SQL statement to retrieve the necessary record.