ASP Lesson 4: Saving time with include files.

A nice feature of Active Server Pages is the ability to include files from various locations into a single page. This lesson will introduce you to the two types of include constructs.

Review: Opening the Connection to the Datasource

To start out this lesson, lets go over a small review. Below is a piece of code. What is the purpose of the code?

' open the connection
' assign recordset object
set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
cn.Open "Asp101"

Answer: create both a Connection and Record object, assign those objects to variables, then open a connection to the DSN Asp101.

Creating Include Files

What is an Include file? As the name suggests, it's a file that's inserted into the calling page at run-time. As the ASP is processed on the server, #include strings are parsed. Then, the server side script is processed. Finally, the page is delivered to the client.

Include files can contain any number of items, whether they are straight HTML, Server side scripts, Cascade Style Sheets, etc. The important thing to remember is the order of execution. Include files are inserted into the page, then processed for content.

My database driven web sites are frequently accessing a back-end database. I don't enjoy typing the same pieces of code over and over, despite being able to type 60+ wpm. I realized, later than sooner, that some of these repeated steps could be put in one location, then included on each page I where needed them. Below you will see two subroutines that handle opening and closing the database connection, as well as defining my recordset object. The content is stored in a file called subConn.inc:

<script Language=VBScript RunAt=Server>
' Global variables
dim cn ' Connection Object
dim rs ' RecordSet Object

sub openCN
   ' open the connection
   ' assign recordset object

   set cn = Server.CreateObject("ADODB.Connection")
   set rs = Server.CreateObject("ADODB.Recordset")
   cn.Open "Asp101"
end sub

sub closeCN
   'close the connection
   cn.Close
   set rs = Nothing
   set cn = Nothing
end sub
</script>

Using Include Files

There is another file that we should include which is called adovbs.inc, which  contain ADO constants for VBScript. We can include these files in a page by having the following lines above the <html> tag.  (The actual file locations may vary.)

<!--#include file="adovbs.inc"-->    Download adovbs.inc
<!--#include file="subConn.inc"-->   Download subConn.inc

There are two methods for including files, virtual and file. The main difference between the two is how you specifiy the file you want to include. File includes cannot start with a forward slash ("/"). Here are some examples of both virtual and file includes:

Virtual examples:
#include virtual="/includes/adovbs.inc"
#include virtual="/includes/constants/adovbs.inc"

File examples:
#include file="includes/adovbs.inc"
#include file="../includes/subConn.inc"

For the remainder of this class we will be using include files to set the connection and open the datasource.