Displaying Listboxes and Hyperlinks with GetString, Part 2By Robert Monteiro
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
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!
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).
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
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!