Using Built-In ASP Components

What’s a Component?

A component is just a piece of code someone wrote so you wouldn’t have to re-invent the wheel. Just like when you create a subroutine, you use the same code over and over again instead of having to re-write virtually the same piece of code somewhere else.

Why use Components?

Components make programming easier. You don’t have to use components, but you will be creating much more work for yourself then needed.

Using the Ad Rotator Component

What’s the Ad Rotator Component? Just what is says. It rotates your ads for you. In order to use this component, you will need three things:

1. The rotator schedule file (text file)

2. ASP page that redirects the user

3. The ASP page that houses the banners

First, let’s create the schedule file. A schedule file will have the following format:

schedulefile.txt

redirect.asp

width

height

border

*

ImageURL

HomePageURL

AltText

Weight

Image2URL

HomePage2URL

AltText

Weight

The first 4 lines of code apply to all banners.

The first line of code tells the browser what file to go to once the banner it clicked. This could be a redirecting script that logs all hits out from your site.

The second and third lines of code affects the width and height of the banners while the fourth line of code sets a border width around the image.

The fifth line of code (*) indicates where the first section of the file stops and the second starts.

ImageURL is the path of your banner

HomePageURL is the URL that the user is taken to when they click on the banner. This URL is passed to the redirect URL for processing. If there is no site to be taken to, then a hyphen (-) should be used.

AltText is used to display text instead of an image if the image doesn’t load.

Weight is a number between 0 and 10,000 that indicates how many times the ad should be display compared to all other banners in the schedule file. For instance, if you had two banners in the schedule file with the weight of 1o and one banner with the weight of 20, the banner with weight 2o would be shown more times then the first two banners.

Here’s a live example:

schedulefile.txt

REDIRECT out.asp WIDTH 468

HEIGHT 60

BORDER 1

*

ex_pro.gif

http://www.batpros.com

BatPros

20

banner0291.jpg

http://www.wavenom.com

Wavenom

20

Now on to the redirection page. This page will redirect the user to the appropriate site when a banner is clicked.

The Ad Rotator creates a querystring for us to use. When a user clicks on a banner, we will get a querystring with the following information:

redirect.asp?url=homepageURL&image=imageURL

So if we want to redirect the user to the appropriate site, we would use the following line of code:

redirect.asp

<% Response.Redirect Request.QueryString(“URL”)%>

The last file we need to create is the ASP page(s) that will display the banners.

index.asp

<%

Dim objAdRotator

Set objAdRotator = Server.CreateObject(“MSWC.AdRotator”)

%>

<HTML><HEAD>

<TITLE>My Site </TITLE>

</HEAD><BODY>

Please support this site!<BR>

<%= objAdRotator.GetAdvertisement(“schedulefile.txt”) %>

Set objAdRotator = Nothing

<BR>Welcome to my site!!!

</BODY></HTML>

This page displays our banners. If you refresh this page several times, you will see that a different image is displayed. Sometimes, especially with fewer banners to display, the same banner will appear.

The first couple of lines of code creates an instance of the Ad Rotator object and gives it the name of objAdRotator.

To display our image, we use the following code like shown above:

<%= objAdRotator.GetAdvertisement(“schedulefile.txt”) %>

As you can see, our object uses the method GetAdvertisement() with the property of the name of our schedule file.

Using the Content Linker Component

What’s the Content Linker Component? It’s used to manage large, frequently updated sites which have tutorials, etc. To use the Content Linker Component, you will need a text file to create a linking list file.

webpageURL description comment

Each line can contain up to three tab-separated items, like above.

webpageURL is the page you want to link to. Use file names only, URLS that begin with Http:// won’t work.

description is a description of the page.

comment is used as a comment for programmers.

So let’s create a linking list file:

linkinglistfile.txt

tut_fun.asp Functions my functions page

tut_com.asp Components my components page

tut_inc.asp Includes my includes page

Now we need to create our ASP page that will display our pages from our linking list file.

index.asp

<HTML><HEAD>

<TITLE>MY TUTORIALS SECTION</TITLE>

</HEAD><BODY>

Starting ASP<BR>

Welcome to my tutorial selection for programming in asp! Start by clicking on one of the links below…<br>

<%

Dim objLinker

Set ObjLinker = Server.CreateObject(“MSWC.NextLink”)

Response.Write “<P><A HREF='” & objLinker.GetPreviousURL(“linkinglistfile.txt”) & “‘>” & objLinker.GetPreviousDescription(“linkinglistfile.txt”) & “</A>” & ” ” & “<A HREF='” & objLinker.GetNextURL(“linkinglistfile.txt”) & “‘>” & objLinker.GetNextDescription(“linkinglistfile.txt”) & “</A></P>”

Set objLinker = Nothing

%>

</BODY></HTML>

The only code we need to review is the following:

<%

Dim objLinker

Set ObjLinker = Server.CreateObject(“MSWC.NextLink”)

Response.Write “<P><A HREF='” & objLinker.GetPreviousURL(“linkinglistfile.txt”) & “‘>” & objLinker.GetPreviousDescription(“linkinglistfile.txt”) & “</A>” & ” ” & “<A HREF='” & objLinker.GetNextURL(“linkinglistfile.txt”) & “‘>” & objLinker.GetNextDescription(“linkinglistfile.txt”) & “</A></P>”

Set objLinker = Nothing

%>

The code above creates a previous and next link for us. In order for us to accomplish this, we must use the methods GetPreviousURL(), GetPreviousDescription(), GetNextURL() and GetNextDescription() of the NextLink object. They are self explanatory.

Remember to include the code above in all the pages you plan on linking. For instance, tut_fun.asp, tut_com.asp and tut_inc.asp would all have the code above at the bottom of their pages, however, different content.

There are also other methods the NextLink object takes:

GetListCount(listfile) – returns the number of pages the linking list file contains

GetListIndex(listfile) – returns the index number of the current page in linking list file

GetNthURL(listfile, num) – returns the URL of the num page in the linking list file

GetNthDescription(listfile, num) – returns the description of the num page in the linking list file

Using the GetNthURL() and GetNthDescription() methods, we can make a table of contents:

<HTML><HEAD>

<TITLE>MY TUTORIALS SECTION</TITLE>

</HEAD><BODY>

Table of Contents<BR>

<%

Dim objLinker, icount, ipage

Set ObjLinker = Server.CreateObject(“MSWC.NextLink”)

ipages = objLinker.GetListCount(“linkinglistfile.txt”)

For icount = 1 to ipages

Response.Write “<A HREF='” & objLinker.GetNthURL(“linkinglistfile.txt”, icount) & “‘>” & objLinker.GetNthDescription(“linkinglistfile.txt”, icount) & “</A><BR>”

Next

%>

The code above will give us a table of contents using a For…Next Loop with a GetNthURL() and GetNthDescription() method.

Using The Browser Capabilities Component

What’s the Browser Capabilities Component? It’s used to figure out what type of browser and different properties the users browser supports.

To see if a users browser supports JavaScript, use the following code:

<%

Dim objBrowserCom

Set objBrowserCom = Server.CreateObject(“MSWC.BrowserType”)

If objBrowserCom.JavaScript Then

Response.Write “This browser supports JavaScript!”

End If

Set objBrowserCom = Nothing

%>

The Browser Capabilities Component also supports these methods:

Browser Name – (.Browser) Returns a string

Browser Version – (.Version) Returns a string

Check to see if the browser supports frames – (.Frames) Returns a boolean value of true or false

Check to see if the browser supports tables – (.tables) Returns a boolean value of true or false

Check to see if the browser supports background sound – (.BackGroundSounds) Returns a boolean value of true or false

Check to see if the browser supports VBScript – (.VBScript) Returns a boolean value of true or false

Author: Andrew Schools