Solved

Classic ASP - Using Case for including files

Posted on 2014-10-17
12
417 Views
Last Modified: 2014-10-17
Hi Experts,
I would like to include files in my classic ASP page depending on conditions.
I was trying the following, but it doesn't seem to work.
<%
select case rs("Department")
    case "Customer Service"    Response.Write "<!-- #include virtual="includes/CustomerMenu.inc" -->"
    case "Safety"    Response.Write "<!-- #include virtual="includes/SafetyMenu.inc" -->"
    case "Business Center"    Response.Write "<!-- #include virtual="includes/BusinessMenu.inc" -->"
end select
%>

Open in new window

What would be the best method to include files based on condition? Thank you for your help.
0
Comment
Question by:romsom
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40387044
Use Server.Execute instead of include

<%
select case rs("Department")
    case "Customer Service"
	server.execute  "includes/CustomerMenu.inc"
    case "Safety"
	 server.execute "includes/SafetyMenu.inc"
    case "Business Center"
	server.execute   "includes/BusinessMenu.inc"
end select
%>

Open in new window


(though I'm rusty with asp)
0
 
LVL 33

Assisted Solution

by:Big Monty
Big Monty earned 200 total points
ID: 40387046
you'll need to use the Server.Execute command, instead of using the include directive. This is because include files are processed BEFORE the asp code in the page is processed. This should work for you:

<%
select case rs("Department")
    case "Customer Service"    Server.Execute( "includes/CustomerMenu.asp" )
    case "Safety"    Server.Execute( "includes/SafetyMenu.asp) "
    case "Business Center"    Server.Execute( "includes/BusinessMenu.asp" )
end select
%>

Open in new window


note that I changed the extension of your include files to .asp. I'm not positive it'll work on files with an .inc extension
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 100 total points
ID: 40387047
The include files will load regardless of the if/then logic.

What I do is make the includes a function, then call the function as needed.

<!-- #include virtual="/includes/BusinessMenu.asp" -->
<!-- #include virtual="/includes/SafetyMenu.asp" -->
<!-- #include virtual="/includes/CustomerMenu.asp" -->
select case rs("Department")
    case "Customer Service" 
         response.write businessMenu()
    case "Safety"    
        response.write saftewayMenu()
    case "Business Center"
      response.write CustomerMenu()   
end select
%>

Open in new window

Your include /includes/BusinessMenu.asp could simply be the menu
<%
function businessMenu()
    businessMenu = "item1 | item2 | item3 "
end function
%>

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:romsom
ID: 40387061
Thank you all for the quick response, let me try each solution. BRB
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 40387094
The first two solutions are the most efficient, as they only include the files you actually need. Have too many include files, especially ones you do not use, could degrade performance and is bad practice imo
0
 

Author Comment

by:romsom
ID: 40387116
The reason I'm so late with the response is that I encountered a problem.
In the include file I have a recordset.
The menu appears based on the condition, but I get an error message:
Type mismatch: 'rs1'
If I don't use the Case the error message doesn't appear, the menu I include is displayed correctly.
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 40387124
I don't see any reference to a variable called "rs1", I only see "rs" so I imagine you have a typo there. Change it to rs
0
 

Author Comment

by:romsom
ID: 40387143
This is my actual code:
<%
select case Department
    case "Customer Service"    Server.Execute( "/SOC/EPS/includes/ProfileMenu.inc" )
    case "Safety"              Server.Execute( "/SOC/EPS/includes/RampProfileMenu.inc" )
    case "Business Center"     Server.Execute( "/SOC/EPS/includes/RampProfileMenu.inc" )
end select
%>

Open in new window


The Department is displayed correctly when I call it on the page like this:
<%=Department%>

There is no rs there.

But the include file contains rs1

I don't get an error when I display the same include file like this:
<!-- #include virtual="/SOC/EPS/includes/ProfileMenu.inc" -->
0
 
LVL 58

Accepted Solution

by:
Gary earned 200 total points
ID: 40387148
server.execute will execute the file as if it was seperate from the calling page so no variables, connections etc are passed

<%
select case rs("Department")
    case "Customer Service"%>
	<!-- #include virtual="includes/CustomerMenu.inc" --><%
    case "Safety"%>
	<!-- #include virtual="includes/SafetyMenu.inc" --><%
    case "Business Center"%>
	<!-- #include virtual="includes/BusinessMenu.inc" --><%
end select
%>

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40387149
Just create your recordsets in the include file but keep your connection string in your main page.

>Have too many include files, especially ones you do not use, could degrade performance
Having several include files is normal.  Look at how wordpress or any other cms works or library.

3 is not even close.  Plus you are only calling a function if needed.  When you actually use these functions you can see the difference and that will probably be nil in your case.  If you are getting 1000 requests an hour, let's talk.

When it comes to navigation, you are calling the db on every load. Speeding things up would be writing the nav to a text/asp file and including that.  Each time you update the navigation in your cms
<%


dim sFileText
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile( Server.MapPath( "BusinessMenu.asp" ) )

do until rs.eof
oFile.WriteLine(rs("item")&" |")
rs.movenext
loop

reponse.write sFile
oFile.Close
Set oFile = nothing
set oFSO = nothing
%>

Open in new window

Just remember to make sure you have given write permissions to where ever you are saving the file.
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 40387169
what file is rs1 being populated in? is it in the include file or somewhere else?

Having several include files is normal.

doesn't necessarily mean it's good practice, having code that's not being used but still is being loaded is a waste of resources, no matter how minimal it is
0
 

Author Closing Comment

by:romsom
ID: 40387179
Thank you so much all of you.
With the help of Gary's solution I got rid of the rs1 error message.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question