ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
What is and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

Server-Side vs. Client-Side Scripting
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Dynamic Include Files

Dynamic Include Files

by Michael Qualls


This is an advanced example and it assumes that you are already familiar with HTML, ASP, and using ActiveX Objects. In it I use the FileSystemObject and TextStream Objects to create "dynamic" includes.

Dynamic Include Files

I think that at one time or another all of us have attempted to create dynamic include files in our ASP applications only to find out that it would not work. The usual approach was to use a variable to hold the name of the file that you wanted to include and then pass the name of that variable to the include directive.

Let us be clear: The following code will not work:

'A variable is declared to hold a file name
Dim MyFile
'The desired file name is passed to the variable.
MyFile = Request("SomeFileName")
'Then the variable holding the file name is passed
'to the include directive.
<!--#include file=<%=MyFile%>-->

The reason that this code listing will not work is because in Active Server Pages the include directives are resolved before the server-side script portions are processed. The above code listing will trigger a nasty error telling you that the include file cannot be found.

One of the reasons to use an include file is to be a container for static HTML that does not normally change (such as a standard header or footer). This file is then referenced using a normal include directive. However, sometimes, different content is needed based upon user input or some other condition. Rather than create an entirely different web page for each and every possible condition, the idea of using "dynamic include" files was born. However, as noted in the code listing above, the most logical way to implement this idea does not work.

In order to get around this problem, the Microsoft FileSystemObject Object can be used to load and pass the content of the desired include file into a string variable which can be inserted into the page that is being sent to the client. The following function, "getFileContents", aids in this process by loading a file that is passed to the function as an input parameter and returning its contents as a string.

'Pass the name of the file to the function.
Function getFileContents(strIncludeFile)
  Dim objFSO
  Dim objText
  Dim strPage
  'Instantiate the FileSystemObject Object.
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  'Open the file and pass it to a TextStream Object (objText). The
  '"MapPath" function of the Server Object is used to get the
  'physical path for the file.
  Set objText = objFSO.OpenTextFile(Server.MapPath(strIncludeFile))
  'Read and return the contents of the file as a string.
  getFileContents = objText.ReadAll
  Set objText = Nothing
  Set objFSO = Nothing
End Function

Using this function "dynamic include" files can be achieved. First, the main page (a template file containing the page layout and any static content) would be loaded and passed to a string variable. Then, the contents of the include file would be loaded and passed to a string variable. Finally, the variable containing the contents of the include file would be inserted into the contents of the variable containing the main page.

Example: Dynamic Include Files

First, lets take a look at the "template" file. In this listing, there is an HTML comment, "<!-- INCLUDE FILE HERE -->". We will replace this HTML comment with the content of the include file.

<h2>Welcome to my web page!</h2>
<table width="500" border="1">

Now, lets look at the include files that will be used for this example. The first include file is the default include file. The default include file is a form that allows the client to choose which of three include files to load. Notice that the "action" attribute of the form is omitted. This is because when the form will be submitting to itself (causing "dynamicinc3.asp" to reload).

<form method="post">
<h3>Select the name of the file you wish to load</h3>
<select id=cboFile name=cboFile>
 <option value="">File #1</option>
 <option value="">File #2</option>
 <option value="">File #3</option>
<input type="submit" value="Submit">

For the sake of this example, the content of the other three include files has been kept very simple.

 <h2 style="color:red">FILE #1 CONTENTS</h2>
 <a href="dynamicinc3.asp">Return to default page</a>
<!-- END INCLUDE FILE #1 -->
 <h2 style="color:green">FILE #2 CONTENTS</h2>
 <a href="dynamicinc3.asp">Return to default page</a>
<!-- END INCLUDE FILE #2 -->
 <h2 style="color:blue">FILE #3 CONTENTS</h2>
 <a href="dynamicinc3.asp">Return to default page</a>
<!-- END INCLUDE FILE #3 -->

Finally, consider the ASP file that makes the whole example run, "dynamicinc3.asp".

'The "getFileContents" function should be included at the top
'of the ASP file.
'Declare variables to hold the content of the main page and
'the include file.
Dim strMain, strInclude
'Get the contents of the main page and pass them to the "strMain"
strMain = getFileContents("")
'Test to see if the "cboFile" select box is being submitted. If so,
'load the requested include file. If not, load the default include.
If Request.form("cboFile") = "" Then
  strInclude = getFileContents("")
  strInclude = getFileContents(Request.form("cboFile"))
End If
'After the proper include file contents are loaded ("strInclude"),
'then insert it into the main page ("strMain") using the "Replace"
strMain = replace(strMain,"<!-- INCLUDE FILE HERE -->",strInclude)
'Use the "Response" Object to "Write" the completed page to the client.
Response.Write strMain

This sample works, and, in effect, creates the ability to use dynamic include files because it does not use the include directive. Instead this example uses the FileSystemObject Object.

This technique can be useful if you wish to keep your layout separated from your content. You can create a template which contains the layout for your website and include files which contain the content of the website. Then using ASP, you can combine the two for client viewing. This is exactly the way that I put my website together!

Let me give you one last thing to think about. How would using techniques similar to this aid you if you are storing your data in XML files?

Related Links

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers