Thursday, November 20, 2008

Beginners III

ASP Including Files


The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.


The #include Directive

You can insert the content of one ASP file into another ASP file before the server executes it, with the #include directive. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.


How to Use the #include Directive

Here is a file called "mypage.asp":

Words of Wisdom:

The time is:

Here is the "wisdom.inc" file:

"One should never increase, beyond what is necessary,

the number of entities required to explain anything."

Here is the "time.inc" file:

<%

Response.Write(Time)

%>

If you look at the source code in a browser, it will look something like this:

Words of Wisdom:

"One should never increase, beyond what is necessary,

the number of entities required to explain anything."

The time is:

11:33:42 AM


Syntax for Including Files

To include a file in an ASP page, place the #include directive inside comment tags:

or

The Virtual Keyword

Use the virtual keyword to indicate a path beginning with a virtual directory.

If a file named "header.inc" resides in a virtual directory named /html, the following line would insert the contents of "header.inc":

The File Keyword

Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file.

If you have a file in the html directory, and the file "header.inc" resides in html\headers, the following line would insert "header.inc" in your file:

Note that the path to the included file (headers\header.inc) is relative to the including file. If the file containing this #include statement is not in the html directory, the statement will not work.

You can also use the file keyword with the syntax (..\) to include a file from a higher-level directory.


Tips and Notes

In the sections above we have used the file extension ".inc" for included files. Notice that if a user tries to browse an INC file directly, its content will be displayed. If your included file contains confidential information or information you do not want any users to see, it is better to use an ASP extension. The source code in an ASP file will not be visible after the interpretation. An included file can also include other files, and one ASP file can include the same file more than once.

Important: Included files are processed and inserted before the scripts are executed.

The following script will not work because ASP executes the #include directive before it assigns a value to the variable:

<%

fname="header.inc"

%>

You cannot open or close a script delimiter in an INC file. This script will not work:

<%

For i = 1 To n

Next

%>

But this script will work:

<% For i = 1 to n %>

<% Next %>

ASP The Global.asa file

previousnext


The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.


The Global.asa file

The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application. All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.

The Global.asa file can contain only the following:

Application events

Session events

declarations

TypeLibrary declarations

the #include directive

Note: The Global.asa file must be stored in the root directory of the ASP application, and each application can only have one Global.asa file.


Events in Global.asa

In Global.asa you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed in event handlers. The Global.asa file can contain four types of events:

Application_OnStart - This event occurs when the FIRST user calls the first page from an ASP application. This event occurs after the Web server is restarted or after the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event.

Session_OnStart - This event occurs EVERY time a NEW user requests his or her first page in the ASP application.

Session_OnEnd - This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specified time (by default this is 20 minutes).

Application_OnEnd - This event occurs after the LAST user has ended the session. Typically, this event occurs when a Web server stops. This procedure is used to clean up settings after the Application stops, like delete records or write information to text files.

A Global.asa file could look something like this:

Note: Because we cannot use the ASP script delimiters (<% and %>) to insert scripts in the Global.asa file, we put subroutines inside an HTML

Global.asa can also be used to control page access.

The example below shows how to redirect every new visitor to another page, in this case to a page called "newpage.asp":

And you can include functions in the Global.asa file.

In the example below the Application_OnStart subroutine occurs when the Web server starts. Then the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a record set from the "customers" table. The record set is assigned to an array, where it can be accessed from any ASP page without querying the database:


Global.asa Example

In this example we will create a Global.asa file that counts the number of current visitors.

The Application_OnStart sets the Application variable "visitors" to 0 when the server starts

The Session_OnStart subroutine adds one to the variable "visitors" every time a new visitor arrives

The Session_OnEnd subroutine subtracts one from "visitors" each time this subroutine is triggered

The Global.asa file:

To display the number of current visitors in an ASP file:

There are <%response.write(Application("visitors"))%>

online now!

ASP Sending e-mail with CDOSYS

previousnext


CDOSYS is a built-in component in ASP. This component is used to send e-mails with ASP.


Sending e-mail with CDOSYS

CDO (Collaboration Data Objects) is a Microsoft technology that is designed to simplify the creation of messaging applications.

CDOSYS is a built-in component in ASP. We will show you how to use this component to send e-mail with ASP.

How about CDONTs?

Microsoft has discontinued the use of CDONTs on Windows 2000, Windows XP and Windows 2003. If you have used CDONTs in your ASP applications, you should update the code and use the new CDO technology.

Examples using CDOSYS

Sending a text e-mail:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.TextBody="This is a message."

myMail.Send

set myMail=nothing

%>

Sending a text e-mail with Bcc and CC fields:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.Bcc="someoneelse@somedomain.com"

myMail.Cc="someoneelse2@somedomain.com"

myMail.TextBody="This is a message."

myMail.Send

set myMail=nothing

%>

Sending an HTML e-mail:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.HTMLBody = "

This is a message.

"

myMail.Send

set myMail=nothing

%>

Sending an HTML e-mail that sends a webpage from a website:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.CreateMHTMLBody "http://www.w3schools.com/asp/"

myMail.Send

set myMail=nothing

%>

Sending an HTML e-mail that sends a webpage from a file on your computer:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.CreateMHTMLBody "file://c:/mydocuments/test.htm"

myMail.Send

set myMail=nothing

%>

Sending a text e-mail with an Attachment:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.TextBody="This is a message."

myMail.AddAttachment "c:\mydocuments\test.txt"

myMail.Send

set myMail=nothing

%>

Sending a text e-mail using a remote server:

<%

Set myMail=CreateObject("CDO.Message")

myMail.Subject="Sending email with CDO"

myMail.From="mymail@mydomain.com"

myMail.To="someone@somedomain.com"

myMail.TextBody="This is a message."

myMail.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

'Name or IP of remote SMTP server

myMail.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpserver") _

="smtp.server.com"

'Server port

myMail.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _

=25

myMail.Configuration.Fields.Update

myMail.Send

set myMail=nothing

%>

No comments:

Your Title