CHAPTER 17
Counters
Chapter 4, Application Object, demonstrated how to instantiate an applicationscoped
variable and use it throughout your application. Such a variable maintains
the same value for every user of your application and lasts until the last user
session ends or until the web server is restarted. Such application-level variables
can be very useful, but what happens when the application ends and restarts? The
value of these application variables must be reinitialized. In that chapter, I
suggested that you could save the application variables to a text file at the end of
the application and reinitialize the variable using the saved value each time the
application is restarted. If you have several application-level variables, this process
can be problematic. Luckily, for numeric variables anyway, there is a better way.
You can use a Counters component.
The Microsoft Counters component allows you to create, increment, decrement,
store, and remove any number of unique counters. You declare one Counters
component for your entire site in GLOBAL.ASA. A Counters object is instantiated
once for your site (not once per application) and, from that time, is limitless in
scope. No matter what session or application is available, the Counters component
is always accessible from anywhere. You need only one Counters object for
your entire site.
As you might guess, a Counters object allows you to create web-site-scoped
counter variables that hold the same value for every user of every application on
your site. For example, suppose you have two different applications defined by
two separate virtual directories. If there is a Counters object instantiated for the
site, a user of Application1 can add a counter to the Counters object and a user of
Application2 can increment or decrement the same counter. The value of all the
counters in the Counters object is saved to the web server’s hard drive (in a file
called Counters.TXT) so if the web server is restarted, you won’t lose the value of
your counter.
310 Chapter 17 – Counters Component
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Accessory Files/Required DLL Files
Counters.DLL
The dynamic link library containing the Counters component. It must be
registered on the web server before it can be instantiated in your web applications.
This DLL is not installed by default when you install IIS.
Counters.TXT
A text file that contains the actual values of the counters that have been
added to the site’s Counters object, if one exists. This is a UTF8-encoded file.
This file can contain any number of counters’ values and should not be edited
manually. The Counters component is hardcoded to look for this file, so don’t
rename it. Also, don’t move it from its installation location, since this will
cause the component to be unable to find it.
Instantiating the Counters Component
To create an object variable containing an instance of the Counters component,
use the Server object’s CreateObject method. The syntax for the CreateObject
method is as follows:
Set objMyObject = Server.CreateObject(strProgId)
where:
• objMyObject represents the name of the Counters object
• strProgId represents the programmatic ID (ProgID) for the Counters component,
which is MSWC.Counters
Example
<SCRIPT LANGUAGE = VBScript RUNAT SERVER>
' The following code uses the Server object's
' CreateObject method to instantiate a Counters
Counters Summary
Properties
None
Collections
None
Methods
Get
Increment
Remove
Set
Events
None
Comments/Troubleshooting 311
Counters
Comments/Troubleshooting
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
' component in the Application_OnStart event
' in the GLOBAL.ASA file.
Sub Application_OnStart
Dim objCounter
Set objCounter = Server.CreateObject("MSWC.Counters")
End Sub
</SCRIPT>
For more detail on the use of the CreateObject method, see its entry in Chapter 8,
Server Object.
Comments/Troubleshooting
The Counters component provides a powerful way to keep track of counters that
are the same throughout your site. It can contain as many counter variables as
memory permits, and each counter name can contain any Unicode character.
The most important thing to remember about the Counters component is that it is
scopeless. This component is basically a repository for “ultraglobal” variables and
should be treated accordingly. Any script in any application that alters a counter’s
value changes that counter’s value for every other script that uses that counter for
the entire site.
One important use for this component is to store and display user vote tallies. For
example, several sites have a quick survey on their home pages. You are asked a
simple question with a few radio buttons for your vote and a Submit button that
allows you to see the vote tallies for each item for which you can vote. This way,
you can maintain the counts for each option indefinitely. The examples in this
chapter demonstrate this concept.
If you are using Windows 9x with Personal Web Server for your development, be
aware that a Counters object is instantiated in the GLOBAL.ASA file created by
Personal Web Server by default. For this reason, you can treat the Counters
component exactly as if it were a built-in object, like the Application, Session, or
Server objects. Note also that although the SCOPE parameter of the OBJECT tag that
you use to instantiate a Counters object has a value of Application, a Counters
component is not limited to application scope.
The following shows how you instantiate a Counters object:
<OBJECT RUNAT="Server" SCOPE = "Application"
ID = "MyCounter" PROGID = "MSWC.Counters">
</OBJECT>
312 Chapter 17 – Counters Component
Methods Reference
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Methods Reference
Get
objCounter.Get(strCounterName)
Retrieves the current value of any counter held in the Counters component. If the
counter name you provide is not yet stored by the Counters component, a new
counter is added and given a value of zero (0).
Parameters
strCounterName
A string that represents the name of the counter variable you wish to manipulate.
This name can contain any Unicode character.
Example
The following example assumes a Counters object already exists (see “Instantiating
the Counters Component” earlier in this chapter) and demonstrates the use
of the Get method. It assumes a Counters component (gobjOptionCounter) has
been instantiated elsewhere.
<HTML>
<HEAD>
<TITLE>Favorite Games</TITLE>
<BODY>
<%
' Dimension local variables.
Dim intDoom
Dim intQuake
Dim intQuake2
' Initialize the preceding variables using the current
' values of the corresponding counters in the Counters
' object (instantiated elsewhere).
intDoom = gobjOptionCounter.Get("FavGameCounter_Doom")
intQuake = gobjOptionCounter.Get("FavGameCounter_Quake")
intQuake2 = gobjOptionCounter.Get("FavGameCounter_Quake2")
' Display the current vote tallies for favorite game.
%>
Here are the current vote counts for favorite game:<BR>
<TABLE WIDTH = 50%>
<TR>
<TD WIDTH = 50%>
Doom
<TD>
<TD WIDTH = 50%>
<%= intDoom %>
<TD>
</TR>
<TR>
Increment 313
Counters
Increment
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
<TD WIDTH = 50%>
Quake
<TD>
<TD WIDTH = 50%>
<%= intQuake %>
<TD>
</TR>
<TR>
<TD WIDTH = 50%>
Quake 2
<TD>
<TD WIDTH = 50%>
<%= intQuake2 %>
<TD>
</TR>
</TABLE>
</BODY></HTML>
Notes
The value of a counter is limited to the range of an integer. Note that the number
of counters held in the Counters component has little effect on the memory it
holds on the web server, since the values of its counters are written to the hard
drive.
Increment
objCounter.Increment(strCounterName)
Increments a counter in the Counters component. If you attempt to increment a
counter that does not yet exist, the counter is created and its value is set to 1. The
new value of the counter is returned.
Parameters
strCounterName
A string that represents the name of the counter variable you wish to manipulate.
This name can contain any Unicode character.
Example
The following example assumes a Counters object (gobjOptionCounter) has
been instantiated elsewhere (see “Instantiating the Counters Component” earlier in
this chapter) and demonstrates the use of the Increment method.
<HTML>
<HEAD>
<TITLE>Favorite Games</TITLE>
<BODY>
<%
' The following line of code increments the
' FavGameCounter_Doom counter in the gobjOptionCounter
' object and returns the new value of the counter.
314 Chapter 17 – Counters Component
Remove
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
' Note that if FavGameCounter_Doom does not yet exist
' in the gobjOptionCounter object, the returned value
' is 1.
%>
You are user number
<%= gobjOptionCounter.Increment("FavGameCounter_Doom") %>
to vote for Doom as your favorite game.<BR>
%>
...[additional HTML and code]
Remove
objCounter.Remove(strCounterName)
Removes a counter from the Counters component and deletes its entry from the
Counters.TXT file. This method has no return value.
Parameters
strCounterName
A string that represents the name of the counter variable you wish to remove.
This name can contain any Unicode character.
Example
The following example demonstrates the use of the Remove method of the
Counters object. This example assumes a Counters object (gobjOptionCounter)
has been instantiated elsewhere.
<%
' The following code removes the FavGameCounter_Wolf3D
' counter from the gobjOptionCounter object.
gobjOptionCounter.Remove("FavGameCounter_Wolf3D")
%>
Notes
See the explanation of the Get method earlier in this chapter.
Set
objCounter.Set(strCounterName, intCounterValue)
The Set method allows you to create a counter in the Counters component and
add its entry to the Counters.TXT file. The new counter’s value is returned.
Parameters
strCounterName
A string that represents the name of the counter variable you wish to manipulate.
This name can contain any Unicode character.
Set 315
Counters
Set
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
intCounterValue
An integer that represents the new value of the counter variable you wish to
set.
Example
The following example demonstrates the use of the Set method. This example
assumes a Counters object (gobjOptionCounter) has been instantiated
elsewhere.
<%
' The following code sets the value of the
' FavGameCounter_Unreal counter to an arbitrary number
' (high) to inflate its perceived popularity. If it does
' not already exist, it is created and initialized to the
' value 987.
gobjoptionCounter.Set("FavGameCounter_Unreal", 987)
%>
316
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
l example at the end of \par this
\par chapter.
\par 292 Chapter 15 – Content Linking Component
\par GetNextDescription
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par GetNextDescription
\par objNextLink.GetNextDescription(strContentLinkList )
\par Returns a string containing the description of the next document listed \par in the
\par Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <HTML>
\par <HEAD>
\par <TITLE>Document List</TITLE>
\par <BODY>
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strNextDesc
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the next item in the
\par ' Content Linking list file.
\par strNextDesc = _
\par objNextLink.GetNextDescription("/MyContentLinkList.txt")
\par ' Display the next description to the client.
\par %>
\par <%= strNextDesc%>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current document is not listed in the Content Linking list file, \par the description
\par text for the last item in the list file is returned by default. If the \par current item is the
\par last item in the list, calling GetNextDescription returns an empty string \par (“”).
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetNextURL 293
\par Content Linking
\par GetNextURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par GetNextURL
\par objNextLink.GetNextURL(strContentLinkList)
\par Returns a string containing the URL entry of the next document listed \par in the
\par Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strNextDesc
\par Dim strNextURL
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the next item in the
\par ' Content Linking list file.
\par strNextDesc = _
\par objNextLink.GetNextDescription("/MyContentLinkList.txt")
\par ' Retrieve a URL for the next item in the Content Linking
\par ' list file.
\par strNextURL = _
\par objNextLink.GetNextURL("/MyContentLinkList.txt")
\par ' Use strNextURL to create a link to the item whose
\par ' description you retrieved using GetNextDescription.
\par %>
\par <A HREF = "<%= strNextURL %>"><%= strNextDesc%></A>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current document is not listed in the Content Linking list file, \par the URL text
\par for the last item in the list file is returned by default.
\par Using GetNextURL with a Content Linking file, you do not have to change \par the
\par code within your HTML to update a “NEXT PAGE” hyperlink, for \par example. You
\par 294 Chapter 15 – Content Linking Component
\par GetNthDescription
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par have only to change the Content Linking list, and the component will automatically
\par update this link for you.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetNthDescription
\par objNextLink.GetNthDescription(strContentLinkList, intItemIndex)
\par Returns a string containing the description for the item in the Nth position \par (on the
\par Nth line) in the Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par intItemIndex
\par An integer indicating the index of the item whose description you wish \par to
\par retrieve from the Content Linking list.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim intTotalCount
\par Dim intCounter
\par ' Instantiate a NextLink object for this script.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a total count of items in the Content Linking file.
\par intTotalCount = _
\par objNextLink.GetListCount("/MyContentList.TXT")
\par ' Iterate through all the items in the Content Linking
\par ' list and generate an entry in an ordered list.
\par %>
\par <OL>
\par <UL>
\par <%
\par For intCounter = 1 to intTotalCount
\par %>
\par <LI>
\par <!-- Create a hyperlink to the URL for the current item -->
\par <!-- in the list. -->
\par <a href "<%=
\par objNextLink.GetNthURL("/MyContentList.TXT", _
\par intCounter)%>">
\par <!-- Retrieve the text description for that URL. -->
\par GetNthURL 295
\par Content Linking
\par GetNthURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par objNextLink.GetNthDescription("/MyContentList.TXT", _
\par intCounter)
\par </LI>
\par <%
\par Next
\par %>
\par </UL>
\par </OL>
\par <%
\par ' Release the memory held for the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par Notes
\par If there is not an item in the position sent in the intItemIndex parameter, \par an
\par error results. To prevent this, you can compare the value to be supplied \par as the
\par intItemIndex argument with the value returned by a call to the GetListCount
\par method.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetNthURL
\par objNextLink.GetNthURL(strContentLinkList, intItemIndex)
\par Returns a string containing the URL for the item in the Nth position (on \par the Nth
\par line) in the Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par intItemIndex
\par The index of the item in the Content Linking list whose URL you wish to
\par retrieve. This is an integer parameter.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim intTotalCount
\par Dim intCounter
\par ' Instantiate a NextLink object for this script.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a total count of items in the Content
\par ' Linking file.
\par intTotalCount = _
\par 296 Chapter 15 – Content Linking Component
\par GetPreviousDescription
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par objNextLink.GetListCount("/MyContentList.TXT")
\par ' Iterate through all the items in the Content Linking
\par ' list and generate an entry in an ordered list.
\par %>
\par <OL>
\par <UL>
\par <%
\par For intCounter = 1 to intTotalCount
\par %>
\par <LI>
\par <!-- Create a hyperlink to the URL for the current -->
\par <!-- item in the list. -->
\par <a href "<%=
\par objNextLink.GetNthURL("/MyContentList.TXT", _
\par intCounter)%>">
\par <!-- Retrieve the text description for that URL. -->
\par objNextLink.GetNthDescription("/MyContentList.TXT", _
\par intCounter)
\par </LI>
\par <%
\par Next
\par %>
\par </UL>
\par </OL>
\par <%
\par ' Release the memory held for the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par Notes
\par If there is not an item in the position indicated by the intItemIndex \par parameter,
\par an error results. To prevent this, you can compare the value to be supplied \par as the
\par intItemIndex argument with the value returned by a call to the GetListCount
\par method.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetPreviousDescription
\par objNextLink.GetPreviousDescription(strContentLinkList )
\par Returns an ASCII string containing the description of the previous document \par listed
\par in the Content Linking list.
\par GetPreviousURL 297
\par Content Linking
\par GetPreviousURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <HTML>
\par <HEAD>
\par <TITLE>Document List</TITLE>
\par <BODY>
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strPrevDesc
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the previous item in
\par ' the Content Linking list file.
\par strPrevDesc = _
\par objNextLink.GetPreviousDescription("/MyContentLinkList.txt")
\par ' Display the previous description to the client.
\par %>
\par <%= strPrevDesc%>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current page cannot be found in the Content Linking list file, \par the description
\par text for the first item in the list file is returned by default. If the \par current item is the
\par first item in the list, calling GetPreviousDescription will return an \par empty string (“”).
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetPreviousURL
\par objNextLink.GetPreviousURL(strContentLinkList)
\par Returns a string containing the URL entry of the previous document listed \par in the
\par Content Linking list.
\par 298 Chapter 15 – Content Linking Component
\par GetPreviousURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strPrevDesc
\par Dim strPrevURL
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the previous item in
\par ' the Content Linking list file.
\par strPrevDesc = _
\par objNextLink.GetPreviousDescription("/MyContentLinkList.txt")
\par ' Retrieve a URL for the previous item in the Content
\par ' Linking list file.
\par strPrevURL = _
\par objNextLink.GetPreviousURL("/MyContentLinkList.txt")
\par ' Use strNextURL to create a link to the item whose
\par ' description you retrieved using GetPreviousDescription.
\par %>
\par <A HREF = "<%= strPrevURL %>"><%= strPrevDesc%></A>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current page cannot be found in the Content Linking list file, \par the URL text
\par for the first item in the list file is returned by default.
\par Using GetPreviousURL with a Content Linking file, you do not have to change \par the
\par code within your HTML to update a “PREVIOUS PAGE” hyperlink, \par for example.
\par You only have to change the Content Linking list, and the component will \par automatically
\par update this link for you.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par Content Linking Component Example 299
\par Content Linking
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par Content Linking Component Example
\par The following example code demonstrates a complete Content Linking component
\par example in one place to illustrate the overall mechanism of the Content
\par Linking component and its accessory content list file.
\par The scenario is simple. The following set of scripts demonstrates the \par dynamic
\par construction of the first few pages of an online book introducing programming.
\par There are five content files (Content1.ASP through Content5.ASP). For \par each file,
\par you want to provide your users with an indicator of current page number \par (out of
\par the total number of pages), a previous-page link, and a next-page link. \par You know
\par the content files will change and pages will be inserted and removed often. \par This is
\par a good example of a programming problem in which the Content Linking component
\par can help.
\par The following script is the HTML version of the fourth page in our online \par book:
\par <HTML>
\par <HEAD><TITLE>Introduction to Programming: Lesson 4 Looping</
\par TITLE></HEAD>
\par <BODY>
\par Welcome to the Introduction to Programming, Lesson 4:
\par Looping.<BR>
\par [TEXT ABOUT LOOPING AND LOOP STRUCTURES]
\par <!-- Begin navigation section construction -->
\par <HR>
\par You are currently viewing page # 4 of 5.<BR>
\par Use the following links to navigate:<BR>
\par <A HREF "Content3.asp">Previous: Lesson 3 Variables</A><BR>
\par <A HREF "Content1.asp">Home: Lesson 1 Introduction</A><BR>
\par <A HREF "Content5.asp">Next: Lesson 5 Pointers</A><BR>
\par <!-- End navigation section construction -->
\par </BODY>
\par </HTML>
\par This HTML page could be easily created by hand and kept up-to-date manually
\par when pages are inserted and removed. However, you can see that with many
\par pages, such upkeep would be tedious, at best. For example, suppose we \par had to
\par insert a page (Lesson 3a: Advanced Variables) between lessons 3 and 4. \par To do this
\par manually, everything in bold in the previous example would have to be \par changed
\par by hand. If we removed the current home page and added a new one (with \par a
\par different name and description), we would have to make even more changes.
\par The Content Linking component can help us here. We start by creating a \par Content
\par Linking list, whose filename is ONLINE_CONTENT_LIST.TXT:
\par Content1.asp Lesson 1 Background
\par Content2.asp Lesson 2 Code Style
\par Content3.asp Lesson 3 Variables
\par Content4.asp Lesson 4 Looping
\par Content5.asp Lesson 5 Pointers
\par 300 Chapter 15 – Content Linking Component
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par The file contains one line for each of our five content pages. Each line \par consists of
\par a filename and a file description, separated by a Tab character. We can \par now add
\par the following code to the navigation section of each page in our online \par book:
\par <!-- Begin navigation section construction -->
\par <HR>
\par <%
\par Dim objContentLink
\par Set objContentLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve the index of the current page.
\par intCurrentPageNumber = _
\par objContentLink.GetListIndex("ONLINE_CONTENT_LIST.TXT")
\par ' Retrieve the total number of pages.
\par intTotalPageCount = _
\par objContentLink.GetListCount("ONLINE_CONTENT_LIST.TXT")
\par ' Retrieve the URL for the first page in the series.
\par strHomeURL = _
\par objContentLink.GetNthURL("ONLINE_CONTENT_LIST.TXT", 1)
\par ' Retrieve the description for the first page in the series
\par strHomeDesc = objContentLink.GetNthDescription( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par ' If the current page index is greater than 1 (i.e., it
\par ' is after the home page), then retrieve information
\par ' about the previous page.
\par If intCurrentPageNumber > 1 Then
\par ' Retrieve the description for the first page in the
\par ' series.
\par strPrevURL = objContentLink.GetPreviousURL( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par ' Retrieve the description for the previous page in
\par ' the series.
\par strPrevDesc = objContentLink.GetPreviousDescription( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par End If
\par ' If the current page index is less than the total page
\par ' count (i.e., it is before the last page), then retrieve
\par ' information about the next page.
\par If intCurrentPageNumber < intTotalPageCount Then
\par ' Retrieve the URL for the previous page in the series.
\par strNextURL = objContentLink.GetNextURL( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par ' Retrieve the description for the next page in the series.
\par strNextDesc = objContentLink.GetNextDescription( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par Content Linking Component Example 301
\par Content Linking
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par End If
\par ' Now use the preceding information to construct the
\par ' navigation section of the current page.
\par %>
\par You are currently viewing page #
\par <%=intCurrentPageNumber%> of
\par <%=intTotalPageCount%>.<BR>
\par Use the following links to navigate:<BR>
\par <%If intCurrentPageNumber > 1 Then%>
\par <A HREF "<%=strPrevURL%>">Previous:
\par <%=strPrevDesc%></A><BR>
\par <%End If%>
\par <A HREF "<%=strHomeURL%>">Home: <%=strHomeDesc%></A><BR>
\par <%If intCurrentPageNumber < intTotalPageCount%>
\par <A HREF "<%=strNextURL%>">Next: <%=strNextDesc%></A><BR>
\par <%End If%>
\par <!-- End navigation section construction -->
\par If we were to replace the following code in bold:
\par <HTML>
\par <HEAD><TITLE>Introduction to Programming: Lesson 4 Looping</
\par TITLE></HEAD>
\par <BODY>
\par Welcome to the Introduction to Programming, Lesson 4:
\par Looping.<BR>
\par [TEXT ABOUT LOOPING AND LOOP STRUCTURES]
\par <!-- Begin navigation section construction -->
\par <HR>
\par You are currently viewing page # 4 of 5.<BR>
\par Use the following links to navigate:<BR>
\par <A HREF "Content3.asp">Previous: Lesson 3 Variables</A><BR>
\par <A HREF "Content1.asp">Home: Lesson 1 Introduction</A><BR>
\par <A HREF "Content5.asp">Next: Lesson 5 Pointers</A><BR>
\par <!-- End navigation section construction -->
\par </BODY>
\par </HTML>
\par with the Content Linking component code segment preceding it, the result \par would
\par be a navigation links section that stays current with the Content Linking \par list. All
\par you would have to do to update the links is to update the Content Linking \par list file.
\par We could even go one step further and save the previous code as an include \par file
\par (called NavConstruct.INC) and include it anywhere in the content pages \par for our
\par online book:
\par <HTML>
\par <HEAD><TITLE>Introduction to Programming: Lesson 4 Looping
\par </TITLE></HEAD>
\par 302 Chapter 15 – Content Linking Component
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par <BODY>
\par Welcome to the Introduction to Programming, Lesson 4:
\par Looping.<BR>
\par [TEXT ABOUT LOOPING AND LOOP STRUCTURES]
\par <!-- #INCLUDE FILE = NavConstruct.INC-->
\par </BODY>
\par Now suppose we must add a page between pages 3 and 4. All that we must \par do,
\par after creating the page itself (and including our NavConstruct.INC include \par file), is
\par to update the ONLINE_CONTENT_LIST.TXT Content Linking list file:
\par Content1.asp Lesson 1 Background
\par Content2.asp Lesson 2 Code Style
\par Content3.asp Lesson 3 Variables
\par Content3a.asp Lession 3a Advanced Variables
\par Content4.asp Lesson 4 Looping
\par Content5.asp Lesson 5 Pointers
\par All the links constructed using the Content inking list component are \par updated
\par upon the code’s execution and the links stay correct. You can avoid \par the task of
\par going into each affected file and updating hardcoded links. It is all \par done for you.
\par 303
\par Content Rotator
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par } t #