Solved

Classic ASP - Using Case for including files

Posted on 2014-10-17
12
408 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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