Dynamic Includes in ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreemati
    New Member
    • Jun 2007
    • 42

    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

    [Code=ASP]

    <%If (Session(“accou nttype”)=1)% Then>
    <!--#include file="footer1.a sp" -->
    <%Else%>
    <!--#include file="footer1.a sp" -->
    <%End If %>

    [/Code]

    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:

    [Code=ASP]
    <% Dim selectInclude
    selectInclude = Session("sccoun ttype")

    Select Case selectInclude

    Case "99" %>
    <!-- #include file="footer1.a sp" -->

    <%Case "2"%>
    <!-- #include file="footer2.a sp" -->

    <%Case "3"%>
    <!-- #include file="footer2.a sp" -->

    <%End Select%>
    [/CODE]

    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:

    [CODE=ASP]
    <% Dim selectInclude

    If Session("Accoun tType") = 1 Then
    selectInclude = 1
    Else
    selectInclude = 2
    End If

    Select Case selectInclude

    Case "1" %>
    <!-- #include file="footer1.a sp" -->

    <%Case "2"%>
    <!-- #include file="footer2.a sp" -->

    <%End Select%>

    [/CODE]

    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
    Sree
    Last edited by jhardman; Jun 11 '07, 05:40 PM. Reason: fixed code tags
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    Sree,

    First code should work fine besides that the % is in the wrong place:
    [code=asp]<%
    If Session(“accoun ttype”)=1 Then %>
    <!--#include file="footer1.a sp" -->
    <%
    Else %>
    <!--#include file="footer2.a sp" -->
    <%
    End If %>
    [/code]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
    [code=asp]session("accoun ttype")=1[/code]
    and [code=asp]session("accoun ttype")="1"[/code]Does this make sense?

    Jared

    Comment

    Working...