iwats asp
Start
 
News
 
Course Notes
Intro
What is ASP?
Uses
Requirements
Basic syntax
Response
Request
IF and CASE
Includes
Sessions
Common Errors
Using Your Code
 
Course Test
 
Resources
Includes

Includes allow you to modularise your code, whether it be ASP or HTML, to make it reusable. By modularising your code, it also makes updates easier, especially on large sites. Below are the two syntax forms for the include command

Command Description
<!-- #include file = "file.htm" --> This will include files in a relational sense, in relation to where the ASP script is.
<!-- #include virtual = "/file.htm" --> This would include the file file.htm from the root of your site

It's important to note that included files are processed and inserted before ASP on the main page is executed. Below is a practical example of using an include:

<HTML>
<BODY>
I am going to include an include here
<P>
<!-- #include file="include.htm" -->
<P>
Finished my including
</BODY>
</HTML>

Now, to code the include file. Include files can include ASP and HTML and can be named with any extension. However, you should use .ASP because files with other extensions can be downloaded, revealing your source code. This is especially important later when you use databases or if you include ASP code in your include files.

I am the include. Here is some <B>Bold</B>.

The resulting page sent to the client's browser would be:

<HTML>
<BODY>
I am going to include an include here
<P>
I am the include. Here is some <B>Bold</B>.
<P>
Finished my including
</BODY>
</HTML>

The most common use of includes is menu/layout control and dynamic inclusion of HTML (usually for secure parts of the site). Above was a simplified example of menu/layout control. A more advanced example is that of dynamic includes. Below is a web page with 2 links:

<HTML>
<HEAD>
<TITLE>Dynamic Includes</TITLE>
</HEAD>
<BODY>
<H1>Dynamic Includes</H1>
<A HREF="dyn.asp?include=1">Include #1</A>
<BR>
<A HREF="dyn.asp?include=2">Include #2</A>
</BODY>
</HTML>

Below is the code for dyn.asp dyn.asp uses a SELECT CASE to check against the request.querystring value.

<HTML>
<BODY>
This is our dynamic include page. Below will be displayed our include
<P>
<%
' dynamic includes are basically a simple variable on previous IF...THEN and CASE SELECT
' actions. Except, we use the include command instead of response.write or the like

SELECT CASE request.querystring("include")
CASE "1"
%>
<!-- #include file = "include1.htm" -->
<%
CASE "2"
%>
<!-- #include file = "include2.htm" -->
<%
CASE ELSE
response.write "You're being naughty and trying to break the script."
END SELECT
%>
<P>
Finished including
</BODY>
</HTML>

You'll notice that that the example "breaks out" of ASP code by closing the %> brackets when writing the include commands. This is because include commands aren't strictly ASP.