Home

ASP ADVANCED

The Content Rotator Component is very similar to the Ad Rotator component.  However, unlike the Ad Rotator component, this component is used to randomly display different HTML content on a Web page.  Here are some ideas for how this component can be used:

  • Tip of the day.  You can use this component to randomly display different tips for using your Web site.  For example, Visit our news group to exchange messages with other users.
  • News flash.  This component can be used to rotate through a list of news events.  For example, Active Server Pages Unleashed now available in stores!
  • Random link.  You can use this component to display a random link drawn from a list of your favorite Web sites.
  • Banner advertisement.  Like the Ad Rotator component, this component can be used to display banner advertisements.  However, using this component, you have greater flexibility over how the advertisement is displayed.

To use the Content Rotator component to display an HTML content string, you use the component's ChooseContent( ) method. TheChooseContent( ) method retrieves an HTML string from a special file called the Content Schedule file and displays it in an Active Server Page, as shown in the following example:

<HTML>
<HEAD><TITLE> Home Page </TITLE></HEAD>
<BODY>
<%
Set MyContent=Server.CreateObject("MSWC.ContentRotator")
%>
<%=MyContent.ChooseContent("content.txt") %>
</BODY>
</HTML>

In this example, an instance of the component is first created by calling the CreateObject( ) method.  Next, an HTML content string is retrieved from the Content Schedule file named content.txt and displayed on the Web page.  Whenever the page is requested, a different HTML string may be displayed.

The Content Schedule File

The Content Schedule file is used to contain all the HTML content strings.  This file is a normal text file, which can be created and edited with any text editor.  It can also be given any name.  Listing below shows an example.

Listing The content.txt file.

%%#2 // Here is the first entry
<FONT COLOR="RED"> Visit Our News Group! </FONT>
%%#3 // Here is the second entry
<B>Don't Forget To Bookmark This Web Site. </B>
%%#5 // Here is the third entry
Download the following free software from our Download Page:
<UL>
<LI> ActiveX Components
<LI> Link Checker
<LI> HTML Validator
</UL>

This Content Schedule file contains three entries.  The beginning of each entry is marked by a double percent sign (%%).  Whenever the choosecontent( ) method is called, one of these entries is retrieved.

In this example, each entry is given a certain weight.  This determines the relative frequency at which a particular entry will be chosen by the Choosecontent( )  method.  You indicate a weight by providing a number after the number sign(#).  For example, the first entry is given a weight of 2.

The weight of an entry can be any number between 0 and 65,535.  If an entry has a weight of 0, it's never shown (this is useful for temporarily disabling an entry).  The higher the weight, the more likely an entry will be retrieved by the Choosecontent( ) method.  If you don't specify a weight for an entry, the entry will have a weight of 1.

In this example, the first entry will be displayed 2 out of every 10 times that the Choosecontent( ) method is called.  The second entry will be displayed 3 out of 10 times, and the third entry will be displayed 5 out of 10 times.  To determine how often an entry will be displayed, divide the weight of that entry by the sum of the weights of all of the entries.

Each entry also includes a comment. For example appropriately enough, the first entry includes the comment "Here is the first entry". To include a comment, simply use two slash characters(//) before the comment.  The comment won't be displayed on the page when the HTML content string is retrieved.

Finally, each entry contains an HTML content string.  This string can span multiple lines.  It can include any HTML tags.  For example, the first entry displays the string visit Our News Group! in red.  The last entry contains a list of software that can be downloaded from the Web site.

Both comments and weights are optional.  A minimal Content Schedule file would contain nothing but HTML content strings that are separated with the %% characters.  In that case, every content string would be displayed with the same frequency.

Dumping the Contents of the Content Schedule File

The Content Rotator component includes one additional method.  By using the GetAllContent( ) method, you can retrieve all the HTML content strings from the Content Schedule file.  Here's an example of how this method is used:

<HTML>
<HEAD><TITLE> Content Schedule File Contents </TITLE></HEAD>
<BODY>
<%
Set MyContent=Server.CreateObject("MSWC.ContentRotator")
%>
<%=MyContent.GetAllcontent("content.txt") %>
</BODY>
</HTML>

When this Active Server Page is displayed, all the entries in the Content Schedule file named content.txt are included in the page.  Entries are automatically separated by horizontal rules with the HTML <HR> tag.

Why would you want to do this?  In a number of situations, this method may prove useful.  For example, if you're using the Content Rotator component to randomly display links to Web sites, you may want to give the user an option to view all the links.

The GetAllContent( ) method is also useful for debugging a Content Schedule file.  If you want to test the appearance of all the entries in this file, you can use this method to view its contents before the entries are displayed to the world.