Access client variable within server-tags in vbscript

我们两清 提交于 2020-01-06 15:29:07

问题


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:

  1. If an ISAPI filter is installed on the Web site, the ISAPI filters is processed first. This is true for all applications.

  2. 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.

  3. In the requested ASP file, IIS separates the script blocks from the static HTML code blocks, reserving the static code in the response body.

  4. 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.

  5. After the ASP page script blocks are processed, their output is injected into the response body with the static HTML code.

  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!