11.3.2- ASP Pages that Use Content Linker to Hyperlink to the Next Page
Created
by Brendan Doss.
|
| << 11.3.1- Structure of the Content Linking Component | Chapter11 | 11.3.3- Adding Descriptions and Previous to Our Link >> |
ASP Pages that Use Content Linker to Hyperlink to the Next Page
Our first example will use the Content Linker simply to show us the next URL based on what ASP finds in the index file. This example uses the NewProductsTour.txt file from the last Try-It-Out.
Try It Out – A Simple Content Linker Example
This example uses the index file, CL-NewProductsTour.txt, CL-NavigationFooter, and five other ASP files that are the pages we want the visitor to walk through on her tour. This looks like a lot of code, but the five ASP files are similar enough that you can cut and paste.
1. We will start with the footer that will hold the navigation code. Open your editor of choice, type in the following and save in your BegASPFiles folder as CL-Navigation.asp. If you are downloading files from the Wrox website, the file below will be CL-Navigaton01.asp; rename it to CL-Navigaton.asp.
<%
Response.Write "<HR>"
Dim MyPageNext
Dim objNL
Set objNL = Server.Createobject("MSWC.NextLink")
MyPageNext = objNL.GetNextURL("CL-NewProductsTour.txt")
Response.Write "Next: <A href='" & MyPageNext & "'>Click here for next item </A>"
%>
2. Open your editor of choice, type in the following and save in your BegASPFiles folder as CL-Hat.asp.
<% Option Explicit %>
<HTML>
<HEAD>
<TITLE>Hat</TITLE>
</HEAD>
<BODY>
<P>Hat of the Week:</P>
<P>Featuring an expanding elastic rim</P>
<!--#include file="CL-Navigation.asp"-->
</BODY>
</HTML>
3. Now we need to make very similar pages for the next four items of clothing that we sell. To make life easier we will just change a few lines in the CL-Hat.asp page and use SaveAs to make that our Sweater page. After you have saved the above Hat file, use the editor to change it to the following and use SaveAs to store this file in the same directory as CL-Sweater.asp:
<% Option Explicit %>
<HTML>
<HEAD>
<TITLE>Sweater</TITLE>
</HEAD>
<BODY>
<P>Sweater of the Week:</P>
<P>Perfect for yachting in navy blue</P>
Just in from Pat's Sweaters</P>
<!--#include file="CL-Navigation.asp"-->
</BODY>
</HTML>
4. Once you have saved the sweater file, repeat the above step to create three more files as follows.
A file named CL-Tie.asp.
<TITLE>Tie</TITLE>
...
<P>Tie of the Week:</P>
<P>Power Yellow, for that bid presentation</P>
A file named CL-Trouser.asp.
<TITLE>Trouser</TITLE>
...
<P>Trousers of the Week:</P>
<P>With special pockets for PDAs</P>
A file named CL-Vest.asp.
<TITLE>Vest562</TITLE>
...
<P>Vest of the Week:</P>
<P>Flowered Vest for dancing around the MayPole</P>
5. Now, test your Hat page by opening your browser of choice and typing in the URL http://my_server_name/BegASP/CL-Hat.asp. Click on the Next hyperlink, to move forward through the tour.
|
|
How It Works
Let's look at the file CL_Hat.asp first. In fact, it looks very similar to the HTML file CL-Hat-NoContentLinking.asp that we saw earlier in this chapter, when considering the benefits of using Content Linker. However, instead of the hard-coded <A HREF> we have inserted the CL-Navigation.asp file, which contains the code of interest:
<HTML>
<HEAD>
<TITLE>Hat</TITLE>
</HEAD>
<BODY>
<P>Hat of the Week:</P>
<P>Featuring an expanding elastic rim</P>
<!--#include file="CL-Navigation.asp"-->
</BODY>
</HTML>
Within CL-Navigation.asp we create a horizontal line so you can see where the navigation area begins, then Dim a variable and an object. We use the Set statement to create an instance of the NextLink component, named objNL:
<%
Response.Write "<HR>"
Dim MyPageNext
Dim objNL
Set objNL = Server.Createobject("MSWC.NextLink")
Then, we ask objNL to use a method called GetNextURL and leave VBScript:
MyPageNext = objNL.GetNextURL("CL-NewProductsTour.txt")
%>
The GetNextURL method will perform five steps:
- Make a note of the URL of the current page
- Open the index file, CL-NewProductsTour.txt
- Find the line of the index file that shows the URL of the current page
- Move down one line in the index file to the next line
- Return to your current page the URL of the next line
In our original file, CL-less-Hat501.htm the HREF names the hyperlink target directly as CL-Sweaters.asp, using the following line:
<A href="CL-Sweater.asp">Click here for next item</A>
Now, with our added ASP code, it's much smarter:
Response.Write "Next: <A href='" & MyPageNext & "'>Click here for next item </A>"
This line is a little confusing, so let's step through it piece by piece. In our previous example, we explicitly named the target file in our link. In this example, the URL of the target file (the next page on our tour) is held in the variable MyPageNext. We are also still within our script block, so we start with our Response.Write statement, and then the first part of our HTML code. However, when using the <A href='target'> syntax, we need to enclose our target within quotes. This is where things get a little tricky. When using Response.Write, any text or HTML is enclosed in double quotes, so we have to use single quotes to enclose everything within the HREF. We use the & character to join items within the Response.Write statement. We have now recreated our HTML code using a slightly longer Response.Write statement. The result will be the same as if we had typed the URL directly, but there is a huge difference: if we want to change the URL we can do so in the Index file rather then on this page
If you view the source of the page, you will see the end result of our hard work - we have produced a standard HTML link:
|
|
Beginners to ASP are frequently confused about the variables and object here. First, we must supply two (later three) variable names. The first variable name identifies the instance of the NextLink object – in this case, we used objNL. The second name is for the variable which will hold the URL found by the objNL.GetNextURL method – here, I used MyPageNext. The third variable will appear later to hold the URL of the previous page. You should also note that although we get two pieces of data (next and previous URLs) we don't need to create two Content Linker objects here. We have only created a single Content Linker object, namely objNL, and then use two of its methods, namely GetNextURL and GetPreviousURL.
The other four files are the same as CL-Hat.asp in that they include the navigation page. The Content Linker will take care of inserting the appropriate Next URL for whatever page is using the Content Linker. Some folks get confused about which page we mean by 'current', 'next' and 'previous'. Recall that the ASP code is written within a page. That ASP code will be executed on the server in the process of the server building a page to send to the browser. The server considers the current page to be the one that it is now building. If your user clicks on next page then that sends an <A HREF> for the next page to the server, and as that page is being built, its URL becomes the current page for the ASP Content Linker methods finding the next and previous pages.
| << 11.3.1- Structure of the Content Linking Component | Chapter11 | 11.3.3- Adding Descriptions and Previous to Our Link >> |

RSS


