Including files in ASP

If you want to include a file in your ASP page, there are two common ways. Firstly, you can use the #include tag:

and simply change the path to point to the file you want. The page will then behave as if the file contents was in the current page.

Another way is to use the Server.Execute sFile command, which allows you to execute the contents of another ASP file.

However, there are times when you know that you'll want to include external
ASP code within another ASP file, but you won't know which include
file to use until run time. In this case, the usual #include directive
won't help you a bit. Because of the way ASP script is processed
before being sent to the client, you simply can't use a variable in
an #include statement. But here's a sneaky workaround: use the
FileSystemObject to read the include file's content and flush it out to
the browser yourself, like this:

<%
dim strFile, fso, fsoFile
strFile = request.queryString("file")
If strFile <> "" then
  set fso = createObject("Scripting.FileSystemObject")
  set fsoFile = fso.openTextFile(strFile)
  '<pre> preserves the original format of the include file
  response.write "<pre>" & fsoFile.readAll & "</pre><br/>"
  set fso = nothing
  set fsoFile = nothing
end if
%>

You might also like...

Comments

ElementK Journals

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson