In my ASP tutorial you will learn about ASP, and how to execute scripts on your server.

ASP Tutorial

ASP Content Rotator (ASP 3.0)



Examples


The
Content Rotator Component


This component displays a different HTML content string each time a user visits
or refreshes the page.




ASP Content Rotator Component


The ASP Content Rotator component creates a ContentRotator
object that displays a different HTML content string each time a user enters or
refreshes a page. A text file, called the Content Schedule File, includes the
information about the content strings.


The content strings can contain HTML tags so you can display
any type of content that HTML can represent: text, images, colors, or
hyperlinks.


Syntax








<%

Set cr=Server.CreateObject( "MSWC.ContentRotator" )

%>

The following example displays a different content each time a
user views the Web page. Create a text file named "textads.txt" in
your default Web Site folder, in a subfolder called text.


"textads.txt":








%% #1

This is a great day!!



%% #2

<h1>Smile</h1>



%% #3

<img src="smiley.gif">



%% #4

Here's a <a
href="http://www.w3schools.com">link.</a>

Notice the #number at the beginning of each content string.
This number is an optional parameter that indicates the relative weight of the
HTML content string. In this example, the Content Rotator will display the first
content string one-tenth of the time, the second string two-tenths of the time,
the third string three-tenths of the time, and the fourth string four-tenths of
the time.


Then, create an ASP file, and insert the following code:








<html>

<body>



<%

set cr=server.createobject("MSWC.ContentRotator")

response.write(cr.ChooseContent("text/textads.txt"))

%>



</body>

</html>

The ASP Content Rotator Component's methods are described
below:


Methods




















Method Description Example
ChooseContent Gets and displays a content string <%

dim cr

Set cr=Server.CreateObject("MSWC.ContentRotator")

response.write(cr.ChooseContent("text/textads.txt"))

%>

Output:



Smiley


GetAllContent Retrieves and displays all of the content strings in
the text file
<%

dim cr

Set cr=Server.CreateObject("MSWC.ContentRotator")

response.write(cr.GetAllContent("text/textads.txt"))

%>

Output:




This is a great day!!




Smile




Smiley




Here's a link.









ASP Content Linking Component



Examples


The
Content Linking Component


This example builds a table of contents.


The
Content Linking Component 2


The example uses the Content Linking Component to navigate between the pages in
a text file.




ASP Content Linking Component


The ASP Content Linking component is used to create a quick
and easy navigation system!


The Content Linking component returns a Nextlink object that
is used to hold a list of Web pages to be navigated.


Syntax








<%

Set nl=Server.CreateObject( "MSWC.NextLink" )

%>

First we create a text file - "links.txt". This file
contains the pages to be navigated. The pages must be listed in the same order
you want them to be displayed, and it also must contain a description for each
file name (use the tab key to separate file name from description). Note:
If you want to add a page to the list or change the order of the pages in the
list; all you have to do is to modify the text file! The navigation will
automatically be correct!


"links.txt":








asp_intro.asp ASP Intro

asp_syntax.asp ASP Syntax

asp_variables.asp ASP Variables

asp_procedures.asp ASP Procedures

On each of the pages listed above, put one line of code:
<!-- #include file="nlcode.inc"-->. This line will include the
code below on every page listed in "links.txt" and the navigation will
work.


"nlcode.inc":









<%
'Use the Content Linking Component
'to navigate between the pages listed
'in links.txt

dim nl
Set nl=Server.CreateObject("MSWC.NextLink")
if (nl.GetListIndex("links.txt")>1) then
Response.Write("<a href='" & nl.GetPreviousURL("links.txt"))
Response.Write("'>Previous Page</a>")
end if
Response.Write("<a href='" & nl.GetNextURL("links.txt"))
Response.Write("'>Next Page</a>")
%>


The ASP Content Linking Component's methods are described
below:


Methods


















































Method Description Example
GetListCount Returns the number of items listed in the Content
Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetListCount("links.txt")

Response.Write("There are ")

Response.Write(c)

Response.Write(" items in the list")

%>

Output:


There are 4 items in the list


GetListIndex Returns the index number of the current item in the
Content Linking List file. The index number of the first item is 1. 0 is
returned if the current page is not in the Content Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetListIndex("links.txt")

Response.Write("Item number ")

Response.Write(c)

%>

Output:


Item number 3


GetNextDescription Returns the text description of the next item listed in
the Content Linking List file. If the current page is not found in the
list file it returns the text description of the last page on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNextDescription("links.txt")

Response.Write("Next ")

Response.Write("description is: ")

Response.Write(c)

%>

Next description is: ASP Variables


GetNextURL Returns the URL of the next item listed in the Content
Linking List file. If the current page is not found in the list file it
returns the URL of the last page on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNextURL("links.txt")

Response.Write("Next ")

Response.Write("URL is: ")

Response.Write(c)

%>

Next URL is: asp_variables.asp


GetNthDescription Returns the description of the Nth page listed in the
Content Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNthDescription("links.txt",3)

Response.Write("Third ")

Response.Write("description is: ")

Response.Write(c)

%>

Third description is: ASP Variables


GetNthURL Returns the URL of the Nth page listed in the Content
Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNthURL("links.txt",3)

Response.Write("Third ")

Response.Write("URL is: ")

Response.Write(c)

%>

Third URL is: asp_variables.asp


GetPreviousDescription Returns the text description of the previous item
listed in the Content Linking List file. If the current page is not
found in the list file it returns the text description of the first page
on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetPreviousDescription("links.txt")

Response.Write("Previous ")

Response.Write("description is: ")

Response.Write(c)

%>

Previous description is: ASP Variables


GetPreviousURL Returns the URL of the previous item listed in the
Content Linking List file. If the current page is not found in the list
file it returns the URL of the first page on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetPreviousURL("links.txt")

Response.Write("Previous ")

Response.Write("URL is: ")

Response.Write(c)

%>

Previous URL is: asp_variables.asp







ASP Browser Capabilities Component



Examples


The
Browser Capabilities Component


This example shows how to determine the type, capabilities and version number of
each browser visiting your site.




ASP Browser Capabilities Component


The ASP Browser Capabilities component creates a BrowserType
object that determines the type, capabilities and version number of each browser
that visits your site.


When a browser connects to a server, an HTTP User Agent Header
is also sent to the server. This header contains information about the browser
(like browser type and version number). The BrowserType object then compares the
information in the header with information in a file on the server called
"Browscap.ini".


If there is a match between the browser type and version
number sent in the header and the information in the "Browsercap.ini"
file, you can use the BrowserType object to list the properties of the matching
browser. If there is no match for the browser type and version number in the
Browscap.ini file, it will set every property to "UNKNOWN".


Syntax








<%

Set MyBrow=Server.CreateObject("MSWC.BrowserType") 

%>

The example below creates a BrowserType object in an ASP file,
and displays a table showing some of the capabilities of the current browser:









<html>
<body>

<%
Set MyBrow=Server.CreateObject("MSWC.BrowserType")
%>

<table border="1" width="100%">
<tr>
<th>Client OS</th>
<th><%=MyBrow.platform%></th>
</tr><tr>
<td >Web Browser</td>
<td ><%=MyBrow.browser%></td>
</tr><tr>
<td>Browser version</td>
<td><%=MyBrow.version%></td>
</tr><tr>
<td>Frame support?</td>
<td><%=MyBrow.frames%></td>
</tr><tr>
<td>Table support?</td>
<td><%=MyBrow.tables%></td>
</tr><tr>
<td>Sound support?</td>
<td><%=MyBrow.backgroundsounds%></td>
</tr><tr>
<td>Cookies support?</td>
<td><%=MyBrow.cookies%></td>
</tr><tr>
<td>VBScript support?</td>
<td><%=MyBrow.vbscript%></td>
</tr><tr>
<td>JavaScript support?</td>
<td><%=MyBrow.javascript%></td>
</tr>
</table>

</body>
</html>


Output:









































Client OS WinNT
Web Browser IE
Browser version 5.0
Frame support? True
Table support? True
Sound support? True
Cookies support? True
VBScript support? True
JavaScript support? True






The Browscap.ini File


The "Browsercap.ini" file is used to declare
properties and to set default values for browsers.


This section is not a tutorial on how to maintain
"Browsercap.ini" files, it only shows you the basics; so you get an
idea what a "Browsercap.ini" file is all about.


The "Browsercap.ini" file can contain the following:









[;comments]
[HTTPUserAgentHeader]
[parent=browserDefinition]
[property1=value1]
[propertyN=valueN]
[Default Browser Capability Settings]
[defaultProperty1=defaultValue1]
[defaultPropertyN=defaultValueN]








































Parameter Description
comments Optional. Any line that starts with a semicolon are
ignored by the BrowserType object
HTTPUserAgentHeader Optional. Specifies the HTTP User Agent header to
associate with the browser-property value statements specified in
propertyN. Wildcard characters are allowed
browserDefinition Optional. Specifies the HTTP User Agent header-string
of a browser to use as the parent browser. The current browser's
definition will inherit all of the property values declared in the
parent browser's definition
propertyN Optional. Specifies the browser properties. The
following table lists some possible properties:


  • ActiveXControls - Support ActiveX® controls?
  • Backgroundsounds - Support background sounds?
  • Cdf - Support Channel Definition Format for
    Webcasting?

  • Tables - Support tables?
  • Cookies - Support cookies?
  • Frames - Support frames?
  • Javaapplets - Support Java applets?
  • Javascript - Supports JScript?
  • Vbscript - Supports VBScript?
  • Browser - Specifies the name of the browser
  • Beta - Is the browser beta software?
  • Platform - Specifies the platform that the browser
    runs on

  • Version - Specifies the version number of the
    browser


valueN Optional. Specifies the value of propertyN. Can be a
string, an integer (prefix with #), or a Boolean value
defaultPropertyN Optional. Specifies the name of the browser property to
which to assign a default value if none of the defined
HTTPUserAgentHeader values match the HTTP User Agent header sent by the
browser
defaultValueN Optional. Specifies the value of defaultPropertyN. Can
be a string, an integer (prefix with #), or a Boolean value

A "Browsercap.ini" file might look something like
this:









;IE 5.0
[IE 5.0]
browser=IE
Version=5.0
majorver=#5
minorver=#0
frames=TRUE
tables=TRUE
cookies=TRUE
backgroundsounds=TRUE
vbscript=TRUE
javascript=TRUE
javaapplets=TRUE
ActiveXControls=TRUE
beta=False

;DEFAULT BROWSER
[*]
browser=Default
frames=FALSE
tables=TRUE
cookies=FALSE
backgroundsounds=FALSE
vbscript=FALSE
javascript=FALSE






ASP AdRotator Component



Examples


Simple
AdRotator Example


This example shows how to use the AdRotator component to display a different
advertisement image, each time a user visits or refreshes the page.


AdRotator
- The Images are Hyperlinks


This example shows how to use the AdRotator component to display a different
advertisement image, each time a user visits or refreshes the page. In addition,
the images are hyperlinks.




ASP AdRotator Component


The ASP AdRotator component creates an AdRotator object that
displays a different image each time a user enters or refreshes a page. A text
file includes information about the images.


Syntax








<%

set adrotator=server.createobject("MSWC.AdRotator")

adrotator.GetAdvertisement("textfile.txt")

%>






Example


Assume we have a file called "banners.asp". It looks
like this:








<html>

<body>

<%

set adrotator=Server.CreateObject("MSWC.AdRotator")

response.write(adrotator.GetAdvertisement("ads.txt"))

%>

</body>

</html>

The file "ads.txt" looks like this:








*

w3schools.gif

http://www.w3schools.com/

Visit W3Schools

80

microsoft.gif

http://www.microsoft.com/

Visit Microsoft

20

The lines below the asterisk in the file "ads.txt"
specifies the images to be displayed, the hyperlink addresses, the alternate
text (for the images), and the display rates in percent of the hits. We see that
the W3Schools image will be displayed for 80 % of the hits and the Microsoft
image will be displayed for 20 % of the hits in the text file above.


Note: To get the links to work when a user clicks on
them, we will have to modify the file "ads.txt" a bit:








REDIRECT banners.asp

*

w3schools.gif

http://www.w3schools.com/

Visit W3Schools

80

microsoft.gif

http://www.microsoft.com/

Visit Microsoft

20

The redirection page (banners.asp) will now receive a
querystring with a variable named URL containing the URL to redirect to.


Note: To specify the height, width, and border of the
image, you can insert the following lines under REDIRECT:








REDIRECT banners.asp

WIDTH 468

HEIGHT 60

BORDER 0

*

w3schools.gif

...

...

The last thing to do is to add some lines of code to the
"banners.asp" file:








<%

url=Request.QueryString("url")

If url<>"" then Response.Redirect(url)

%>

<html>

<body>

<%

set adrotator=Server.CreateObject("MSWC.AdRotator")

response.write(adrotator.GetAdvertisement("textfile.txt"))

%>

</body>

</html>



That's all!!




Properties

























Property Description Example
Border Specifies the size of the borders around the
advertisement
<%

set adrot=Server.CreateObject("MSWC.AdRotator")

adrot.Border="2"

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>
Clickable Specifies whether the advertisement is a hyperlink <%

set adrot=Server.CreateObject("MSWC.AdRotator")

adrot.Clickable=false

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>
TargetFrame Name of the frame to display the
advertisement
<%

set adrot=Server.CreateObject("MSWC.AdRotator")

adrot.TargetFrame="target='_blank'"

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>

Methods















Method Description Example
GetAdvertisement Returns HTML that displays the advertisement in the
page
<%

set adrot=Server.CreateObject("MSWC.AdRotator")

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>





Introduction to ADO



ADO can be used to access databases from your
web pages.




Accessing a Database from an ASP Page


The common way to access a database from inside an ASP page is
to:



  1. Create an ADO connection to a database
  2. Open the database connection
  3. Create an ADO recordset
  4. Open the recordset
  5. Extract the data you need from the recordset
  6. Close the recordset
  7. Close the connection




What is ADO?



  • ADO is a Microsoft technology
  • ADO stands for ActiveX Data Objects
  • ADO is a Microsoft Active-X component
  • ADO is automatically installed with Microsoft IIS
  • ADO is a programming interface to access data in a database




Where to go next?


If you want to study more ADO, read our ADO
tutorial
.



ASP Dictionary Object



The Dictionary object is used to store
information in name/value pairs (referred to as key and item)




Examples


Does
a specified key exist?


This example demonstrates how to first create a Dictionary object, and then use
the Exists method to check if a specified key exists.


Return
an array of all items


This example demonstrates how to use the Items method to return an array of all
the items.


Return
an array of all keys


This example demonstrates how to use the Keys method to return an array of all
the keys.


Return
the value of an item


This example demonstrates how to use the Item property to return the value of an
item.


Set
a key


This example demonstrates how to use the Key property to set a key in a
Dictionary object.


Return
the number of key/item pairs


This example demonstrates how to use the Count property to return the number of
key/item pairs.




The Dictionary Object


The Dictionary object is used to store information in
name/value pairs (referred to as key and item). The Dictionary object might seem
similar to Arrays, however, the Dictionary object is a more desirable solution
to manipulate related data.


Comparing Dictionaries and Arrays:



  • Keys are used to identify the items in a Dictionary object
  • You do not have to call ReDim to change the size of the
    Dictionary object

  • When deleting an item from a Dictionary, the remaining
    items will automatically shift up

  • Dictionaries cannot be multidimensional, Arrays can
  • Dictionaries have more built-in functions than Arrays
  • Dictionaries work better than arrays on accessing random
    elements frequently

  • Dictionaries work better than arrays on locating items by
    their content


The following example creates a Dictionary object, adds some
key/item pairs to it, and retrieves the item value for the key gr:









<%
Dim d
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "re","Red"
d.Add "gr","Green"
d.Add "bl","Blue"
d.Add "pi","Pink"
Response.Write("The value of key gr is: " & d.Item("gr"))
%>

Output:


The value of key gr is: Green 


The Dictionary object's properties and methods are described
below:


Properties

























Property Description
CompareMode Sets or returns the comparison mode for comparing keys
in a Dictionary object
Count Returns the number of key/item pairs in a Dictionary
object
Item Sets or returns the value of an item in a
Dictionary object
Key Sets a new key value for an existing key
value in a Dictionary object

Methods

































Method Description
Add Adds a new key/item pair to a Dictionary object
Exists Returns a Boolean value that indicates whether a
specified key exists in the Dictionary object
Items Returns an array of all the items in a
Dictionary object
Keys Returns an array of all the keys in a
Dictionary object
Remove Removes one specified key/item pair from
the Dictionary object
RemoveAll Removes all the key/item pairs in the
Dictionary object





ASP Folder Object




The Folder Object is used to return information about a specified
folder.





The Folder Object


The Folder object is used to return information about a specified folder.


To work with the properties and methods of the Folder object, you will have
to create an instance of the Folder object through the FileSystemObject object.
First; create a FileSystemObject object and then instantiate the Folder object
through the GetFolder method of the FileSystemObject object.


The following code uses the GetFolder method of the FileSystemObject object
to instantiate the Folder object and the DateCreated property to return the date
when the specified folder was created:





<%
Dim fs,fo
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder("c:\test")
Response.Write("Folder created: " & fo.DateCreated)
set fo=nothing
set fs=nothing
%>

Output:

Folder created: 10/22/2001 10:01:19 AM

The Folder object's collections, properties, and methods are described below:


Collections












CollectionDescription
FilesReturns a collection of all the files in a specified
folder
SubFoldersReturns a collection of all subfolders in a specified
folder

Properties













































PropertyDescription
AttributesSets or returns the attributes of a specified folder
DateCreatedReturns the date and time when a specified folder was
created
href="prop_datelastaccessed_folder.asp">DateLastAccessedReturns the date and time when a specified folder was last
accessed
href="prop_datelastmodified_folder.asp">DateLastModifiedReturns the date and time when a specified folder was last
modified
DriveReturns the drive letter of the drive where the specified folder
resides
IsRootFolderReturns true if a folder is the root folder and false if
not
NameSets or returns the name of a specified folder
ParentFolderReturns the parent folder of a specified folder
PathReturns the path for a specified folder
ShortNameReturns the short name of a specified folder (the 8.3 naming
convention)
ShortPathReturns the short path of a specified folder (the 8.3 naming
convention)
SizeReturns the size of a specified folder
TypeReturns the type of a specified folder

Methods


















MethodDescription
CopyCopies a specified folder from one location to another
DeleteDeletes a specified folder
MoveMoves a specified folder from one location to another
CreateTextFileCreates a new text file in the specified folder and returns a
TextStream object to access the file