When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Sunday, August 01, 1999

The low-down on #includes, Part 3

  • Read The low-down on #includes, Part 1
  • Read The low-down on #includes, Part 2

  • Dynamic Includes: What are they?
    Wouldn't it be neat to be able to write an ASP Script that included a file based on some parameter passed in? For example, you might have fifty text files, with each text file containing a different state's motto. It would be neat to have the user enter his state's abbreviation in a form, and then be taken to an ASP page that used an #include to load in the correct motto. For example:

    - continued -

    'Obtain the user's state
    Dim State
    State = Request.form("StateAbbreviation")

    'Now, load in the correct motto

    <!--#include virtual="/mottos/<%=State%>"-->

    Well, I'd agree that being able to do that would be neat, but the fact of the matter remains that we can't do that! Pure dynamic includes are impossible using just ASP. That is a fact. I hope I haven't ruined your day.

    Dynamic Includes: Why the Hell Can't I Use a Dynamic Include?
    Well, if you'll recall the gritty details presented in the previous article, files are included before the ASP gets a chance to be parsed. This allows us to include files that contain ASP script, something we couldn't do if the ASP script was parsed before the includes were performed. So, since the ASP script is not processed when the includes are performed, we can't expect IIS to know what <%=State%> equals. You'll get an error, telling you it can't find the file /mottos/<%=State%>. Don't give up just yet, there are some workarounds.

    Dynamic Includes: Workarounds
    If you need to include files that have ASP script in them, you are going to have to use a case statement to execute the correct ASP code. For example, let's say you know you will include one of three files, but you don't know what file exactly, since it is determined on some variable named pickFile. If pickFile equals 1, you want to include 1.asp; if it equals 2, include 2.asp, and if it equals 3, you want to include 3.asp. You could do the following:

    Select Case pickFile
    Case 1: %>
    <!--#include file="1.asp"-->
    <% Case 2: %>
    <!--#include file="2.asp"-->
    <% Case 3: %>
    <!--#include file="3.asp"-->
    <% End Select %>

    All three files, 1.asp, 2.asp, and 3.asp, would be loaded through SSI, but the code that was executed would depend on the value of pickFile.

    If you want to include files that have no ASP script, you can use the FileSystemObject to dynamically generate the filename. You can then use FSO to open the file and dump the contents. Check out Charles Carroll's Dynamic Include Lesson or Michael Qualls's Dynamic Include Files article to learn more about this technique.

    Dynamic Include Files with Server.Execute
    With ASP 3.0 you can use the Server.Execute method to kind of fake a dynamic include. With Server.Execute, the ASP page called is loaded, run, and then unloaded. This is great if you have some procedural ASP code you want to execute, but oftentimes developers fill #include files with commonly used functions. Assume that A.asp contains the code: Server.Execute("B.asp"). Because of the nature of Server.Execute, A.asp can't call any functions declared in B.asp! For that reason, Server.Executes are rarely useful for dynamic includes, in my opinion.

    For more information on Server.Execute and ASP 3.0, be sure to read this article!

    So, what have we learned today? We've learned how to include files, using #include file and #include virtual. We've learned about the gritty details of include files, about the advantages of include files, and some recommended conventions. Also, we learned about dynamic includes, and why they're not possible. I hope you've had fun. If you have any questions or comments on this article or on 4Guys or the ASP Messageboard, be sure to send us feedback.

    Happy Programming!

  • Read The low-down on #includes, Part 1
  • Read The low-down on #includes, Part 2

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article