2:17 pm Saturday, July 05, 2008


  Navigation
  Help?
Contact Aaron for all inquiries.
 

  News

ASP Inline Calendar 3.9 Beta 1
ASP Games
ASP Inline Corporate Calendar 3.8 BETA 1 Demo
Calendar Updates
 

A Better World By Design
 
  Creating Your First ASP Page Tutorial
AaronOutpost : ASP / Tutorials / ASP 101 / Include Files

ASP 101: Include Files
By: Aaron B. Copyright AaronOutpost.com

 

    This is part 6 of 6 of ASP 101 by AaronOutpost.com. If you are unsure of what we may be talking about in the tutorial, view the previous parts.

So far we have created your first ASP page, we have defined a value, displayed it on the page, as well as discussed some date and time functions. We then learned about If, Then, and Else statements, and then learned how to log user information such as their IP address. We now going to talk about using Include Files.

Let's begin.

 

Part 6:
INCLUDE FILES

Include files is a great feature and will hopefully become a great resource to yourself. I use include files in all of my application as well as my website. Instead of re-entering the same code over and over again you can create one file and include it on other pages. Such as a page header and footer. You will notice on AaronOutpost.com there is the same header and footer on every page. I used an include file to load header.asp and footer.asp which are in my virtual directory. So how can you do it?

Include file command only works on .ASP files but it is actually entered outside the script tags <% %>.

<!-- This script will fail -->
<%

If view <> "print" Then

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

End If

%>


<!-- This script will work -->

<%

If view <> "print" Then %>

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

<% End If %>

 

The virtual and file keywords indicate the type of path you are using to include the file, and filename is the path and file name of the file you want to include.

Included files do not require a special file name extension; however, it is considered good programming practice to give included files an .inc extension to distinguish them from other types of files. I however do not do this because it makes for easier editing if all files have the same extension .asp. I name my include files inc_filename.asp

 

Using the Virtual Keyword

This uses the virtual directory as a base. I use this for the include files that you see on the left and right side of AaronOutpost.com. Since you will be browsing multiple directories on the website, I needed to have the files link to it's physical place on the server. Let's say you have a my calendar on your website. It would reside in Calendar. You want to include the mini calendar on all of the pages so you would use the following.

 

<!-- #include virtual ="Calendar/inc_mini_calendar.asp" -->

 

Using the File Keyword

Now lets say you want to use the relative directory. Maybe you want the header to change depending on what directory you are in.

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

Now you can still call files from sub directories using the file command.

<!-- #include file ="includes/header.asp" -->

 

So create a header.asp and a footer.asp that contains an html layout for your website. Your include files can also contain ASP script. In the final example I include AaronOutpost.com Header and Footer files.
 

CONGRATULATIONS!!!

Now, you have completed part 5 of 6 Learning ASP 101 by AaronOutpost.com.

Open Viewable Example

<!-- #include virtual ="header.asp" -->

<html>

<head>

<title>My First ASP Page</title>

</head>

<body bgcolor="#FFFFFF" text="#000000" link="#003300" vlink="#006600" alink="#689577">

 

<%

'Dimension variables

Dim MyVariable 'Comment on what this variable holds

 

'Define a value for the MyVariable
MyVariable = "I am learning ASP! This is my first programmed variable!"
%>

<br><br>
<font face="Arial" size="2" color="#000000">
<%
= MyVariable %><br><br>

Server Date & Time is: <%= Now() %><br><br>

Server Time is: <%= Time() %> <br><br>

Server Date is: <%= Date() %><br><br>

Current Hour is: <%= Hour(Now()) %><br><br>

Current Minute is: <%= Minute(Now()) %><br><br>

Current Second is: <%= Second(Now()) %><br><br>

vbGeneralDate: <%= FormatDateTime(Now(), 0) %><br><br>

vbLongDate: <%= FormatDateTime(Now(), 1) %><br><br>

vbShortDate: <%= FormatDateTime(Now(), 2) %><br><br>

vbLongTime: <%= FormatDateTime(Now(), 3) %><br><br>

vbShortTime: <%= FormatDateTime(Now(), 4) %><br><br>

<% Dim intHour 'Variable to hold hour intHour = Hour(Now()) If intHour > 12 Then
'If hour is under 5 pm then afternoon
If intHour < 17 Then %>
Good Afternoon!
<% Else %>
Good Evening!
<% End If
Else %>
Good Morning!
<% End If %>

<%

Dim Temp, IpAddress

Temp = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

If Temp<>"" Then

     IpAddress = Temp

     Else

     IpAddress = Request.ServerVariables("REMOTE_ADDR")

End If


%>

<br><br>My IP Address Is: <%= IpAddress %>

</font>

</body> </html>

<!-- #include virtual ="footer.asp" -->

 


AaronOutpost : ASP / Tutorials / ASP 101 / Include Files



Aaron Outpost West Virginia, USA A Mark of _-Squire-_ Creations Inc.  CopyrightŠ 2005 All Rights Reserved