//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Files/Directories & FSO :: How do I dynamically include files?


How do I dynamically include files?

One of the greatest obstacles faced by ASP developers is the ability to dynamically include files. Since #include directives are processed BEFORE the ASP code, it is impossible to use if/else logic to include files.  
 
Or is it? 
 
Depending on what you are doing within your include files, and how many you are including, it IS possible to use if/then logic to make use of includes. While it is not feasible for all situations, and it is often an inefficient solution, it can occasionally be quite a handy workaround.  
 
Let's start with two sample HTML files, 1.htm and 2.htm. For the sake of simplicity, they contain very simple code:  
 
<!-- 1.HTM: --> 
    <font color=#ff0000>This is 1.htm</font> 
 
<!-- 2.HTM: --> 
    <font color=#0000ff>This is 2.htm</font> 
 
Now, let's set up some conditional includes! For this example, we'll assume you want to include the file 2.htm if your page is passed a parameter of 2, otherwise include 1.htm. Here is an example of how you could accomplish this task:  
 
<% 
    if Request.QueryString("param")="2" then 
%> 
        <!--#include file="2.htm"--> 
<% 
    else 
%> 
        <!--#include file="1.htm"--> 
<% 
    end if 
%>
 
Now try accessing the page in these three ways, and experiment with the results:  
 
    http://yourserver/file.asp?param=1 
    http://yourserver/file.asp?param=2 
    http://yourserver/file.asp 
 
Of course you can perform this kind of include logic based on various conditions, such as the date, the time, or the user's browser.  
 
Please note that in the above example, if you are still using IIS 4.0, BOTH include files will be processed (IIS 5.0 is much smarter about this). So, the more options you have, the less efficient this solution will be. When the number of possible includes start getting a bit high, and the code in the include files is pure HTML (no ASP logic), you could try something like this:  
 
<% 
    if Request.QueryString("param")="2" then  
        filespec = "2.htm"  
    else 
        filespec = "1.htm" 
    end if 
 
    filespec = Server.MapPath(filespec)  
    set fs = CreateObject("Scripting.FileSystemObject")  
    set f = fs.OpenTextFile(filespec)  
    content = f.ReadAll() 
    set f = nothing  
    set fs = nothing  
 
    Response.Write(content)  
%>
 
(If the file has ASP logic, it will be displayed as plain text, because it won't be available to the ASP engine early enough.) 
 
Another option, in IIS 5.0 and up, is to use "dynamic includes" via Server.Execute. Using the above scenario, here is an example: 
 
<% 
    if Request.QueryString("param")="2" then  
        Server.Execute("2.htm") 
    else 
        Server.Execute("1.htm") 
    end if 
%>
 
Please see Article #2006 for some of the common problems people have with the the Server.Execute and Server.Transfer methods.

Related Articles

Can I include a file in both server-side and client-side script?
Can I read / write a user's file without a prompt?
Can I rename a file using FileSystemObject?
Could I get some help working with files using FileSystemObject?
How do I avoid 'the red x' when an image is missing?
How do I change the modified time of a file?
How do I create / manipulate images from ASP?
How do I find the owner, author, and other properties of a file?
How do I get a list of a folder's subfolders?
How do I get the name of the current URL / page?
How do I prevent people from 'leeching' my CSS or JS files?
How do I prevent people from 'leeching' my images?
How do I retrieve a random file from a given folder?
How do I send the correct filename with BinaryWrite?
How do I sort a list of files?
How do I use FileSystemObject to create a file on the client?
Why do I get 'Disk not ready' errors with FileSystemObject?
Why do I get 'Invalid procedure call or argument'?
Why do I get 'Path not found' errors with Scripting.FileSystemObject?
Why do I get 'Permission Denied' errors with FileSystemObject?
Why do I get 800A0034 errors?
Why do I get 800A003E / Input past end of file errors?
Why do I get 800A0BBA errors?
Why do I get 800A0BBC errors?
Why do I get an 'Invalid Path Character' error?
Why do I get permissions errors after upgrading to Windows XP?
Why does FileSystemObject hang all of a sudden?
Why is 'the operation completed successfully' an error message?

 

 


Created: 7/9/2000 | Last Updated: 12/29/2003 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (303)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...