Home

ASP ADVANCED

You can easily include one file in another Active Server Page by using a server-side INCLUDE directive.  A server-side INCLUDE directive shouldn't appear within a script; use it outside a script as part of the HTML code:

<HTML>
<HEAD><TITLE> Welcome </TITLE></HEAD>
<BODY>
<!-- #INCLUDE VIRTUAL="Mybanner.inc"-->
Welcome To Our Web Site!
</BODY>
</HTML>

In this example, the file mybanner.inc is inserted into the Active Server Page below the <BODY> tag.  When this Active Server Page is processed, any scripts or HTML code in the file mybanner.inc are included in the Active Server Page above

You can include one file in another in two ways: by providing a virtual path to the file as in the preceding example, or by providing a physical path to the file.  Following is an example of the latter method:

<HTML>
<HEAD><TITLE> welcome </TITLE></HEAD>
<BODY>
<!-- #INCLUDE FILE="mybanner.inc" -->
Welcome To Our Web Site!
</BODY>
</HTML>

If you supply a physical path to the included file by using the FILE attribute, the file must be located in the current directory or a subdirectory of that directory.  The path of the file is relative to the current directory.  This is a significant limitation; therefore, you will almost always use the VIRTUAL attribute instead.

The included file can have any name and any extension.  By convention, included files usually end with the extension .inc, but you can use .asp, .htm, .html, or any other extension you prefer.

Including one file in another is useful in two situations.  The first and most obvious situation is when you want to display the same content or execute the same script page after page.  For example, it's not uncommon for every page at a Web site to have the same banner and footer.  Instead of repeating the same HTML code over and over, you can simply include a banner and rooter file as part of every page.

You can also include the same Active Server Page script on multiple pages by using the INCLUDE directive.  However, because the INCLUDE directive must occur outside a script, the script you include must be completely contained in script delimiters.  You can't include a fragment of a script.

The second situation in which including one file in another is useful is when you want to simulate server redirection.  To do this, you include one whole Active Server Page in a second Active Server Page.  Consider the following example.

<%
IF Request.Form("FirstName")=" " THEN
%>
<!-- #INCLUDE VIRTUAL="register.asp" -->
<%
Response.End
END IF
%>
<HTML>
<HEAD><TITLE> Registration Results </TITLE></HEAD>
<BODY>
Thank you <%=Request.Form("FirstName")%> for registering!
</BODY>
</HTML>

This example has exactly the same effect as using the Response.Redirect method.  If a user has neglected to enter information into the FirstName field of the registration form, he or she is returned to the registration page.  However, because including a file takes place completely on the server, this simulated redirection is more reliable than actual redirection.

Notice the use of the Response.End method in this example.  The Response.End method is included to prevent the remainder of the Active Server Page from being displayed if the registration page is displayed.

It's important to realize that IIS processes any INCLUDE directives before it processes scripts.  This means that you can't use a script command to dynamically include one file in another.  For example, the following script won't work:

<%
IF Request.Form("FirstName")=" " THEN
MyInclude="register.asp"
ELSE
MyInclude="HomePage.asp"
END IF
%>
<!-- #INCLUDE VIRTUAL="<%=MyInclude%>" -->

This script won't work because the server will attempt to include any files before the script is executed.  This means that the server will attempt to include the file "<%=MyInclude%>", which, of course, doesn't exist.