NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Dynamic Server-Side Includes, Revisited

Written by Eric Smith, Northstar Computer Systems LLC

In a previous tip, I talked about how you can't dynamically include a file in your ASP file, but apparently my explanation was misunderstood, so here goes again. The standard server-side include directive looks something like this:
<!--#include virtual="/includes/header.asp" -->
This type of directive is processed before any ASP code is run, which means you can't dynamically build this statement and essentially can't dynamically choose an ASP file to run. The options presented in other articles where you wrap a block of code or an include with an If/Then statement still doesn't change the fact that both files are loaded into memory.

The alternate option you have if you're just loading client-side HTML/JavaScript code is to read the file using the FileSystemObject and a TextStream object and print it out to the console. Here's a routine I use to do that:

Sub IncludeFile(strFilename)
   If strFilename = "" Then Exit Sub

   On Error Resume Next
   Dim objFSO, objFile, strContents
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.OpenTextFile(Server.MapPath(strFilename), ForReading, False)
   strContents = objFile.ReadAll 
   Response.Write strContents & vbCrLf
   objFile.Close
   Set objFile = Nothing
   Set objFSO = Nothing
End Sub
This code assumes you have the Scripting Runtime referenced using a METADATA tag (see my previous tips as asptechniques.com for more information on this). Note that this code simply reads a file and immediately prints it to the output window. This means that you can't have ASP code in the files you read this way. I typically use this only for plain HTML files or JavaScript code when I need to dynamically specify a filename.

Keywords: [ Uncategorized ASP Tips ]

Publication Date: 2/10/2001