ASP Hosting sale!
1,000MB space, 40GB transfer, 1 SQL Server 2000 db! - $9.95 per month*!
Limited Time Offer!

aspdev | articles | tutorials | forums

ASPdev -> ASP Tutorial -> ASP include files

ASP include files

ASP allows to include files within ASP pages, by using the #include ASP directive.
ASP include files are very useful for creating common pieces of ASP code, which can be reused by many ASP pages and applications. For example a large website with dynamic content might have a common header and footer, which can be separated in 2 files and simply included on all website pages.

ASP Include files syntax

You can insert the content of one file (ASP file, text file, html file) into another ASP file, with the ASP #include directive. When using ASP #include directive the file is inserted before the server actually processes it.

The following code shows how to include a simple text file into an ASP page by using the #include ASP directive along with the file keyword:

Include file code (inc.txt):

Welcome to my website!

ASP page code (default.asp):

<html>
<body>
<!--#include file="inc.txt"-->
<%
‘Some ASP server-side code here
%>
</body>
</html>

When including files with the file keyword we are indicating that the file path is relative to the directory where the main ASP page is (the ASP page where we are including the file).

The following code shows how to include a simple text file into an ASP page by using the #include ASP directive along with the virtual keyword:

Include file code (inc.txt):

Welcome to my website!

ASP page code (default.asp):

<html>
<body>
<!--#include virtual="/includes/inc.txt"-->
<%
‘Some ASP server-side code here
%>
</body>
</html>

When including files with the virtual keyword we are indicating that we are using virtual file path. If the inc.txt file is in a virtual directory called includes, we include this file with the following line of code:

<!--#include virtual="/includes/inc.txt"-->

The most important thing to remember when working with the ASP #include directive is that the included file is inserted on the ASP page before the ASP page is executed.




Contact Us