I'm using IIS 6, and created Two ASP 3.0 files: -Main.asp -Colors.asp
Main.asp
<%
If sColor = true then
Server.Execute "Colors.asp"
End If
'If sColor is true, Pops over to Colors.asp
'Then pops right back over to here again
'Once back here again, it has no idea what
'sRed or sBlue was at all...it's as if has
'been "blank slated"...sRed? Who the heck is sRed?
If sRed then
Response.Write "Color is Red"
End If
'Does not work...skips right over...
'Who is sRed? What is sRed?
'Oh well, keep on truckin'
%>
Colors.asp
<%
Dim sRed
sRed = instr(sString, "Red") >0
Dim sBlue
sBlue = instr(sString, "Blue") >0
Dim sGreen
sGreen = instr(sString, "Green") >0
%>
If one were to go into the Colors.asp file above and modify/append it to read as follows:
<%
Dim sRed
sRed = instr(sString, "Red") >0
Dim sBlue
sBlue = instr(sString, "Blue") >0
Dim sGreen
sGreen = instr(sString, "Green") >0
If sRed then
Response.Write "Color is Red"
End If
%>
One would receive a screen with "Color is Red" when sColor was true over at Main.asp and sString contained "Red." So I know she's getting over there, and also returning back over to Main.asp...but somehow she has no clue about those variables: sRed, sBlue, or sGreen that were dimmed over at Colors.asp. Once she gets back over to Main.asp she's clueless.
What gives? Why does she have ASP Amnesia once she gets back to Main.asp after having just been over at Colors.asp?
Edit
Dear YougoTiger,
I did what you suggested (I think) in Main.asp:
If sColor = true then
Server.Execute "Colors.asp"
If sRed then
Response.Write "Color is Red"
End If
End If
Nothing...still has ASP Amnesia- sRed Who?
Edit 2
Dear Bitwize,
I'm using Server.Execute instead of #include in order to free up the server. As you know #includes always get handled first in ASP, regardless of whether they are inside an If
block. I'm basically trying to do dynamic server-side includes by way of Server.Execute, which can in fact be placed within an If
block according to Microsoft:
Microsoft Support - Using the Server.Execute Method
The New ASP 3.0 Server Methods (Note: this is was originally http://www.15seconds.com/issue/010220.htm but that site is gone, the link is now a redirect to a numeric domain. Feel free to do your own search for the article by Paul Litwin and update this link if you can find it.)
A Look at ASP 3.0
The low-down on #includes
I still needing help with this, or a better explanation as to what is going on in my particular case. I've got a lot of dimmed variables over in that Color.asp file that I don't want the server to bother with if sColor=False.
Don't think it can be done without some changes. Using server.execute, the 2 pages are completely unaware of each others local variables (see proof below). An include would be better although you do loose the conditional ability.
The Request / Response Objects are shared by both files so you can work with those as need be. As far as sending variables between the files your only option is session or application variables. See below for code sample.
file 1:
dim localParent
localParent="start Value"
Response.write("Parent:"&localParent&"<br>")
Server.Execute "file.asp"
Response.write("Parent after Exec:"&localParent&"<br>")
file 2:
Response.write("Child:"&localParent&"<br>")
localParent = "Child changed"
Response.write("Child Afer set:"&localParent&"<br>")
Output:
Parent:start Value
Child:
Child Afer set:Child changed
Parent after Exec:start Value
Session variable passing: file 1:
Dim myVar
'Do stuff
session("myVar") = myVar ' Save variable in session
Server.Execute "file.asp"
myVar = session("myVar") 'get changed variable back out of session
file 2:
Dim myLocalVar
myLocalVar = session("myVar") ' Get variable from session
'Do stuff to myLocalVar
session("myVar") = myLocalVar 'Put variable back into session for calling page to use.
I'm wondering if this might be a scope issue? I'm seeing two possible places that you're running into scope issues:
First, since your sRed in in another file, is scope limited to the file when you run a server.execute?
Second, since you're calling server.execute inside a if then, I'd expect that your other file has a scope of between the if and endif . You're then checking sRed AFTER the endif. Of the two, I'd think the second was more likely. Try your
If sRed then
Response.Write "Color is Red"
End If
block inside your first if then block, after the server.execute and see if this is the case.
Bitwize is completely correct - The calling page has no access to the executed pages local variables and neither does the executed page have access to the calling pages local variables. If you need to share local variables then you need to use include directives.
I have built entire ASP site architectures using the Server.Execute()
technique and each executed script executes effectively in isolation. The only variable sharing you can do is via the Application
or Session
objects both of which have issues.
The only other way you might achieve your desired effect is via HTTP requests in your code to other scripts and have those scripts return JSON/XML structures with the relevant information, but IMHO this would add far more overhead than just using include directives.
精彩评论