Classic ASP - Using Case for including files

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.
romsomIT DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
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
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

romsomIT DeveloperAuthor Commented:
Thank you all for the quick response, let me try each solution. BRB
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
romsomIT DeveloperAuthor Commented:
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
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
romsomIT DeveloperAuthor Commented:
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
GaryCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
romsomIT DeveloperAuthor Commented:
Thank you so much all of you.
With the help of Gary's solution I got rid of the rs1 error message.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.