Dynamic Includes

The most important thing to note is that server side include files are inserted into their holding file before any of the holding file is processed. The include file is parsed before any ASP code so you can't use ASP variables in server side includes like the example 1 below. In example 1 below the variable 'whichpage' will not exist at the time the include file is parsed.

Example 1:

<%
Dim whichpage
whichpage = Request.QueryString("page")
If whichpage = "" Then
whichpage = "default"
%>
<!-- #include file="<%= whichpage %>.asp" -->
<%End If %>

The code in example 1 is trying to dynamically generate the name of the include file. We were trying to send a string through the querystring and assign it to the variable 'whichpage' and then it was our intention that that variable would go with the .asp file extension and make up an asp file name. Unfortunately, since the web server first inserts the include file, it will attempt to include a file with the name <%= whichpage %>.asp . This will generate an error as that file does't exist.

The way I would suggest that we go about dynamic includes is to include all files and use some conditional statement to make sure that only one will actually be executed. The following code should work:

Example 2:

<%
Dim whichpage
whichpage = Request.QueryString("page")
Select Case whichpage
Case "default"
%>
<!-- #include file="default.asp" -->
<% Case "main" %>
<!-- #include file="main.asp" -->
<% Case "admin" %>
<!-- #include file="admin.asp" -->
<% Case Else %>
<!-- #include file="default.asp" -->
<%End Select%>

The above code will include all three include files but depending on the value of our variable 'whichpage' only one file makes up the outputed page.

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Get your best asp web hosting provider now and save 25%