I don't believe this is possible. Include files get processed before ASP so it can't work that way... you could put the if statement inside the include file though.
Actually, that will work just fine...
<%if 1 = 2 then%>
<!--#INCLUDE VIRTUAL="/inc/fileA.asp"-->
<%else%>
<!--#INCLUDE VIRTUAL="/inc/fileB.asp"-->
<%end if%>
The actions/results/output of the selected INCLUDE will appear on your web page.
The problem is, YES -- BOTH files get executed prior to one of them being displayed. So, it's a bit inefficient.
But, if you go the (admittedly preferred) Server.Execute("/fileB.asp") route, you may run into some other trouble. Here's why:
When you create variables on your page, the INCLUDE files have full access to them... so, the code inside your INCLUDE can use those variables to alter their results.
Files which are EXECUTED cannot access page-variables... nor can they pass page-variables back to be used by the main page AFTER the code is executed. Executes are entirely self-contained. They DO, however, have access to the Server variables, and the Request object... so you CAN have things like <%if request.querystring("varName") = "bob" then%> in your execute file.
The best thing about executes (IMHO) is that the code is ONLY executed IF it is the selected code. Also, the execute command accepts a variable name, so the following would work just fine, too:
<%
select case varName
case 1
incFile = "/inc/case1.inc"
case 2
incFile = "/inc/errormsg.inc"
case 3
incFile = "/people/webmaster_bio.htm"
case else
incFile = "default.inc"
end select
Execute is not meant to be a replacement for a traditional include.
IMHO, this is exactly the type of scenario that server.execute was designed to handle.
It also helps design a logically structured application.
Having IIS process all your include files when they are not needed is a very poor design and you will most definately get a significant performance boost by replacing your "conditional" includes, with server.execute logic.
Includes were never designed to be used in conditional processing. They were designed for encapsulating some reusable chunk of code.
I think Mr3putt has provided you a great demonstration of how to use conditional includes efficiently.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.