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