Published: Thursday, October 26, 2000
Displaying Listboxes and Hyperlinks with GetString, Part 2
By Robert Monteiro
Read Part 1
In Part 1 we looked at how to use GetString to generate and
display a listbox. In this part we'll look at how to create hyperlinks!
Creating Data-Driven Hyperlinks with GetString
While writing the listbox-populating code we looked at in Part 1, I
realized that I could use GetString to display a list of clients as hyperlinks having a
unique ID! The ID could then be retrieved from the query string and used in a DB query.
For example, I might need to know the details of a company called "Laughing Lumberjack
Lager" having a unique ID which is the primary key. So I display the name of the
company as a hyperlink to a page and append the ID to the link. A bit confusing, so let
me explain with an example.
Consider the following hyperlink:
<a href="display.asp?id=67">Laughing Lumberjack Lager</a>.
If a user clicks on the hyperlnk Laughing Lumberjack Lager, he will see
all the required details about the company Laughing Lumberjack Lager
displayed by the page display.asp. The ASP page display.asp would contain the code
to displays the details for a particular company (specified by the ID passed through
the QueryString). Within the page display.asp lies a line
<% ID = Request.QueryString("id") %>.
Since I now possess the ID of the company, I can use it in my SQL query to
obtain the company details!
Using GetString, we can create a list of hyperlinks from a database table!
To display such a hyperlink-list, we only need to make a small change
to the code we looked at in Part 1 (specifically the few lines of
code around the call to GetString and the SQL string).
<%
Option Explicit
'Create the SQL query
Dim mySQL
mySQL="select FAQCategoryID, Name from tblFAQCategory order by Name"
'Create the Connection and execute the query.
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "DSN=MyDSN"
Dim objRS
Set objRS = objConn.Execute(mySQL)
'Incase no records exist, have a handler to present a sweet message
'and call the subroutine CloseConn which closes the connection.
If objRS.eof then
Response.Write "Sorry, No records were found.<br>"
Call CloseConn
Response.End
End If
'This part of the code creates the various hyperlinks
'selected option for all companies.
Response.write("<a href=""display.asp?ID=")
Response.Write objRS.GetString(,,""">", _
"</a><br><a href=""display.asp?ID=", "-null-")
Response.Write("""></a>")
Call CloseConn 'Clean up!
' Sub to handle closing and cleaning up connections
Sub CloseConn
objRS.close
set objRS = Nothing
objConn.close
Set objConn = Nothing
End Sub
%>
|
[
View Live Demo]
That's all you need to do! Note that the only lines that were changed from our listbox example was the SQL
statement line (we included the unique identifier in the select list) and the three
Response.Write statements that outputted the header, footer, and GetString output.
Pretty neat, eh? I think you will find the above snippet of code quite useful! I've found that there are
many times when I need to quickly create a list of data-dependent hyperlinks. Enjoy!
Happy Programming!
By Robert Monteiro