Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  Include: Basics
     [next Lesson]  Includes: Other Sites, Dynamic FileNames

Includes Files Dynamically

The include files are gathered and processed BEFORE any ASP code. Soif your code looks like this:

<%SELECT CASE
     CASE 1 %>
    <!--#include virtual="whatever1.asp"-->
     CASE 2 %>

    <!--#include virtual="whatever2.asp"-->
     CASE 3 %>
    <!--#include virtual="whatever3.asp"-->
<%END SELECT%>

Three includes are performed before any ASP code is executed.

YOU CANNOT DO:

<%
     whichfile="1"%>
  <!--#include virtual="whatever<%=whichfil%>.asp"-->

Though this is a reasonable idea.

<!--#include virtual="whatever.asp"-->

We however have coded a workaround that is FREE you may find useful. The workaround is:

   filename=/learn/test/includedynamic.asp

<Test Script Below>


<html><head>
<TITLE>includedynamic.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
whichfile="bookscifi.asp"
Call ReadDisplayFile(whichfile)
response.write "<hr>"

whichfile="bookhorror.asp"
Call ReadDisplayFile(whichfile)
response.write "<hr>"


whichfile="/learn/test/bookmarketing.asp"
Call ReadDisplayFile(whichfile)
response.write "<hr>"
%>

</body></html>
<%
SUB ReadDisplayFile(FileToRead)
    whichfile=server.mappath(FileToRead)
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set thisfile = fs.OpenTextFile(whichfile, 1, False)
    tempSTR=thisfile.readall
    response.write tempSTR
    thisfile.Close
    set thisfile=nothing
    set fs=nothing
END SUB
%>

The only downside to this method is no ASP Code ( i.e. anything in <% %> ) will be parsed or executed in the included file. If you must execute ASP code the Win2k server.execute @
/freebook/asp/incwin2k.aspx
or the 3rd party components like ASPHTTP must be employed @
/learn/include/asphttp.asp

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.