?
Solved

Classic ASP - Using Case for including files

Posted on 2014-10-17
12
Medium Priority
?
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 800 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 53

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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