Dynamically Including Files in ASP

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:

strFile = request.queryString("file")

If strFile <> "" then
set fso = createObject("Scripting.FileSystemObject")
set fsoFile = fso.openTextFile(strFile)

 preserves the original format of the include file

response.write "
" & fsoFile.readAll & "


set fso = nothing
set fsoFile = nothing

end if

Source: Jason Fisher
Viewed 14467 times