Codefixer - ASP tutorials, resources and software
ASP.NET 3.5/2.0 Hosting – Click Here
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Saturday 10 May
MembersPro PayPal - ASP Membership software

DiscountASP.NET – ASP.NET 3.5/2.0 Hosting


ASP Partner ASP Partner ASP Partner more partners...

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.

LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software