问题
I have the following block of code that's im running in an asp-page:
<script language="VBScript">
sub ok_onclick()
dim localVariable
localVariable= "hello"
<%
call serversideFunction(localVariable)
%>
end sub
The following block
<% call ServerSideFunction(localVariable) %>
Throws the following error:
An unhandled exception ('Variable is undefined: 'localVariable'') occurred in dllhost.exe [24184]
I've noticed that i can't put my local variables within the servercode-tags.
So my question is, how do send my local variable's value down to the server?
回答1:
This is Server-Side programming 101 which does sometimes catch people out but there are lots of good tutorials and articles out there that can help with understanding the fundamentals.
Image taken from MSDN - ASP Overview
Quote from MSDN - ASP Overview
IIS processes an ASP file in the following order when a request is received from a client:
If an ISAPI filter is installed on the Web site, the ISAPI filters is processed first. This is true for all applications.
If the ASP application contains a Global.asa file in the root directory, the Global.asa is processed. Global.asa files specify event scripts and declare objects that have session or application scope. They do not display content; instead they stores event information and objects used globally by the ASP application.
In the requested ASP file, IIS separates the script blocks from the static HTML code blocks, reserving the static code in the response body.
IIS processes the script blocks. The script blocks might include transaction processing, database access calls, or calls to COM components in which case COM+ handles some of the processing.
After the ASP page script blocks are processed, their output is injected into the response body with the static HTML code.
The response is sent to the client.
The problem boils down to step 6. All the server-side processing is done before the response is sent to the client so in the example provided;
'Client-Side procedure
Sub ok_onclick()
dim localVariable
localVariable= "hello"
<%
'Server-Side code already processed before response
'is returned to the client.
Call serversideFunction(localVariable)
%>
End Sub
The serversideFunction()
server-side function will have already executed before the client response is sent so will never know of the existence of localVariable
.
In these situations you need to pass the localVariable
data back to the Server using various tried and tested techniques (HTML form, AJAX request etc) that all boils down to communicating a HTTP GET
or HTTP POST
request that the Server can interpret and process.
Once you have sent the request the data can be picked up using the ASP Request Object and passed into a Server-side variable.
<%
Dim serverVariable
'Request came via a HTTP GET.
serverVariable = Request.QueryString("localVariable")
'Request came via a HTTP POST.
serverVariable = Request.Form("localVariable")
'Request came via either a HTTP GET or HTTP POST
'Using this has it's overheads.
serverVariable = Request("localVariable")
%>
Useful Links
- MSDN - ASP Overview
来源:https://stackoverflow.com/questions/34920818/access-client-variable-within-server-tags-in-vbscript