ASP Include Files
#include Directive
By using the #include directive, you can insert the contents of another ASP file into this ASP file before the server executes the ASP file.
The #include directive is used to create functions, headers, footers, or other elements that need to be reused on multiple pages.
How to Use the #include Directive
Here is a file named "mypage.asp":
<html>
<body>
<h3>Words of Wisdom:</h3>
<p><!--#include file="wisdom.inc"--></p>
<h3>The time is:</h3>
<p><!--#include file="time.inc"--></p>
</body>
</html>
This is the "wisdom.inc" file:
the number of entities required to explain anything."
This is the "time.inc" file:
Response.Write(Time)
%>
If you view the source code in your browser, it will look like this:
<html>
<body>
<h3>Words of Wisdom:</h3>
<p>"One should never increase, beyond what is necessary,
the number of entities required to explain anything."</p>
<h3>The time is:</h3>
<p>11:33:42 AM</p>
</body>
</html>
Syntax for Including Files
To include a file in an ASP page, place the #include directive inside comment tags:
or
<!--#include file ="somefilename"-->
The Virtual Keyword
Use the virtual keyword to indicate a path that begins with a virtual directory.
If a file named "header.inc" is located in the virtual directory /html, the following line of code will insert the contents of the "header.inc" file:
The File Keyword
Use the file keyword to indicate a relative path. A relative path starts from the directory that contains the referencing file.
If you have a file in the html directory, and the "header.inc" file is located at the head of html, the following line of code will insert the contents of the "header.inc" file into your file:
Note that the path of the referenced file (headersheader.inc) is relative to the referencing file. If the file containing the #include declaration is not in the html directory, this declaration will not take effect.
Tips and Notes
In the section above, we have used ".inc" as the file extension for the referenced files. Note: If a user tries to browse an INC file directly, the contents of this file will be displayed. If the contents of your referenced file contain confidential information or information that you do not want any user to see, it is best to use ".asp" as the extension. The source code in an ASP file is invisible after being compiled. Referenced files can also reference other files, and an ASP file can reference the same file multiple times.
Important:Before the script executes, the referenced file will be processed and inserted. The following script cannot execute because ASP executes the #include directive before assigning a value to the variable:
fname="header.inc"
%>
<!--#include file="<%fname%>"-->
You cannot include file references between script delimiters. The following script cannot execute:
For i = 1 To n
<!--#include file="count.inc"-->
Next
%>
However, this script can execute:
<!--#include file="count.inc" -->
<% Next %>
Other Extensions