Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 20,514 times

Related Categories

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
%>

© 2001 Element K Journals, a division of Element K Press LLC ("Element K"). Element K and the Element K logo are trademarks of Element K LLC

Comments

  • Dynamic includes

    Posted by ccarey on 29 Jul 2002

    Using FSO to dynamicaly include a file only works if you aren't sharing any variables within the included file. A good workaround is using select case like this:

    <%
    SelectPage = Request.QueryStri...