Solved

Classic ASP - Using Case for including files

Posted on 2014-10-17
12
397 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 32

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
 

Author Comment

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

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Set cursor to client side 3 48
Adding Datediff to staistics page 2 35
Need help editing script 3 52
ASP SQL Syntax Duplicate Key 7 67
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 …
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/…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now