Examples of include files in ASP

Include files are a very powerful feature in classic ASP allowing the developer to reuse code and keep common code in one place to make it easier to updte. Here we describe some of the most important uses for include files.

A database connection

Often a database is used from several pages within a website. It makes sense to put the connection details into an include file that can be called from each script that uses the database. This reduces the amount of code needed in each script but more importantly, it means that if any changes are needed to the database connection, only one file needs to be edited. It also keeps the database specific code separate from the more general logic. This can be very useful if development is carried out on a different computer than the main production server where a different database may be used.

The following code is the include file, which we shall call settings.asp. It shows a connection to a MySQL database.

  ServerName = "localhost"
  Database = "DatabaseName"
  User = "UserName"
  Password = "xxxPasswordxxx"
  DataConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=" & ServerName &_
    "; DATABASE=" & Database & "; UID=" &_
    User & ";PASSWORD=" & Password & "; OPTION=3"

The ASP script that uses the database would contain some code as follows, to call the include file and then open the database.

<!--#include file="settings.asp"--%>
  Set DBConn = Server.CreateObject("ADODB.Connection")
  DBConn.Open DataConnection
  SQL = "SELECT * FROM DatabaseTable"
  Set Recordset = Server.CreateObject("ADODB.Recordset")
  Recordset.Open SQL, DBConn, 1, 2

  'Use the recordset


While it is easy enough to copy and paste the connection code into each script that uses it, there are definite advantages in keeping this code separate and in a single file.

A repeating variable on a web site - the VAT rate example

Sometimes a value needs to be displayed in several places in a web site, and that variable may change. Rather than hard coding it into every page, it is easier to set that variable in an include file and call this from each page that uses it. For example, on this web site we need to display the VAT rate, because we sell some commercial components. (VAT is a sales tax applied in the European Union and in recent years the UK government has changed the rate several times.)

The include file is very simple. The rate is shown as a number so that it can be used in calculations as well as displayed.

<% VatRate = 17.5 %>

This include file is called vatrate.asp, and it is used as follows:

<!--#include file="vatrate.asp"-->

<p>The VAT rate is <%= VatRate %>%.</p>

A block of HTML defined in an include file - a menu bar

A menu bar can be stored in an include file if it is going to be used on multiple web pages. Then if a new menu item is to be added, only the include file needs to be edited.

The include file could be called menu.asp, and it can consist of the HTML of the menu without any ASP code, although it will have the .asp extension.

<div id="menucolumn">
<ul id="menu">
  <li><a href="item1.asp">Item 1</a></li>
  <li><a href="item2.asp">Item 2</a></li>
  <li><a href="item3.asp">Item 3</a></li>

In this example the HTML will be inserted inline at the point where the include directive appears, so it is important to put it in the right place. the result will be a file with the menu bar, as though the HTML code was part of the original file.

<!-- #include file="menu.asp" -->


The sample code in these tutorials is written for VBScript in classic ASP unless otherwise stated. The code samples can be freely copied.