469,641 Members | 1,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

Dynamic Includes in ASP

Hi Everyone,

I have newly started as a ASP developer. I have been working on some change request and was blogged down with dynamic includes. Eventually I had managed to figure out the problem and find a solution. I am posting my findings for two purpose:

1) Help someone else who faces the same problem
2) Experts to point me to a better solution to the same porblem.

Problem: The are three different types of user to a system: Admin1, Admin2 and Admin3. All the three user are doing the same admin functionality - Add New user to the system but the admin add new user form should show different footers - footer1 for Admin1 and footer2 for admin2 and 3.

The admin type is determined by accounttype which is a session varible, so I tired the obvious solution

Expand|Select|Wrap|Line Numbers
  2. <%If (Session(“accounttype”)=1)% Then>
  3. <!--#include file="footer1.asp" -->
  4. <%Else%>
  5. <!--#include file="footer1.asp" -->
  6. <%End If %>
The page kept throwing the followign error -
Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'SQLAccess'
footer1.asp, line 74

I tired, the same code with Response.Write and it was working fine. A simple google pointed me to an article explaining about Server Side Include and hence for the same reason I was not able to use the aboce code.

The actual Solution to the problem was thought to be:

Expand|Select|Wrap|Line Numbers
  1. <% Dim selectInclude
  2. selectInclude = Session("sccounttype")
  4. Select Case selectInclude 
  6. Case "99" %>
  7. <!-- #include file="footer1.asp" -->
  9. <%Case "2"%>
  10. <!-- #include file="footer2.asp" --> 
  12. <%Case "3"%>
  13. <!-- #include file="footer2.asp" --> 
  15. <%End Select%>
but again I got error -
Active Server Pages error 'ASP 0126'
Include file not found
createUser.asp, line 669
The include file 'footer2.asp' was not found.

I had no clue why it showed me this error as I was quite sure for the code to work but a little trying around - I figured the EXACT working code is:

Expand|Select|Wrap|Line Numbers
  1. <% Dim selectInclude
  3. If Session("AccountType") = 1 Then 
  4. selectInclude = 1
  5. Else 
  6. selectInclude = 2
  7. End If
  9. Select Case selectInclude 
  11. Case "1" %>
  12. <!-- #include file="footer1.asp" -->
  14. <%Case "2"%>
  15. <!-- #include file="footer2.asp" --> 
  17. <%End Select%>
Questions from experts:
1) Though I have vague idea about why my second piece of code didn't work, I would request a clearer reasoning.
2) Is there any other better way of doing it?

Hope any looking for dynamic include finds it useful and I get more clearer about this concept.

Thanks and wishes
Jun 11 '07 #1
1 1450
3,406 Expert 2GB

First code should work fine besides that the % is in the wrong place:
Expand|Select|Wrap|Line Numbers
  1. <%
  2. If Session(“accounttype”)=1 Then %>
  3.    <!--#include file="footer1.asp" -->
  4. <%
  5. Else %>
  6.    <!--#include file="footer2.asp" -->
  7. <%
  8. End If %>
The error code you showed does not appear to be related to this problem.

One thing that I have seen that causes the logic to fail in these cases is when the variable is supposed to be an integer but is actually a numeric string. In other words you need to distinguish between
Expand|Select|Wrap|Line Numbers
  1. session("accounttype")=1
Expand|Select|Wrap|Line Numbers
  1. session("accounttype")="1"
Does this make sense?

Jun 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.