在VBScript服务器标签内访问客户端变量 [英] Access client variable within server-tags in vbscript

查看:188
本文介绍了在VBScript服务器标签内访问客户端变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code以下块是在ASP页进出口运行:

 < SCRIPT LANGUAGE =VBScript中>
    子ok_onclick()        暗淡所以LocalVariable
        所以LocalVariable =你好        <%
        调用serversideFunction(所以LocalVariable)
        %GT;    分结束

以下块

 <%调用ServerSideFunction(所以LocalVariable)%GT;

抛出了以下错误:


  

未处理的异常('变量未定义:'所以LocalVariable'')
  发生在DLLHOST.EXE [24184]


我注意到,我不能把服务器code-标签内我的局部变量。

所以我的问题是,如何把我的局部变量的值下降到服务器?


解决方案

这是服务器端编程101有时确实抓住人们了,但有很多很好的教程和文章,有可以与理解基本面有所帮助。

经典ASP建筑

MSDN采取图像 - ASP概述


  

<子> MSDN报价 - ASP概述


  
  

IIS处理的时候从客户端接收到一个请求按以下顺序一个ASP文件:


  
  

      
  1. 如果一个ISAPI筛选器安装在网站,ISAPI筛选器首先处理。这是所有应用程序如此。


  2.   
  3. 如果ASP应用程序包含在根目录中的Global.asa文件,在Global.asa被处理。的Global.asa文件指定事件脚本,并声明具有会话或应用程序范围的对象。它们不显示的内容;相反,它们存储由ASP应用程序全局使用事件信息和对象。


  4.   
  5. 在请求的ASP文件,IIS分离从静态HTML code块脚本块,保留静态code在响应主体。


  6.   
  7. IIS处理脚本块。该脚本块可能包括交易处理,数据库访问调用,或COM组件的调用在这种情况下COM +处理一些处理。


  8.   
  9. 在ASP页面脚本块被处理后,它们的输出被注入体内的反应与静态HTML code。


  10.   
  11. 的响应被发送到客户端。


  12.   

问题归结到步骤6.所有服务器端处理完成之前的响应被发送到客户端,以便在所提供的例子;

 客户端程序
子ok_onclick()
  暗淡所以LocalVariable
  所以LocalVariable =你好
  &LT;%
  服务器端code响应之前已经处理
  被返回到客户端。
  呼叫serversideFunction(所以LocalVariable)
  %GT;
结束小组

serversideFunction()服务器端函数将被发送之前,客户端的响应所以永远不会知道所以LocalVariable <的存在已执行/ code>。

在这种情况下,你需要使用各种尝试和测试技术的(HTML表单,AJAX请求等)<到所以LocalVariable 数据传回服务器/ EM >这一切都归结为通信的 HTTP GET HTTP POST 要求服务器能够跨pret和流程。

一旦你发送的数据可以使用的 ASP请求对象,然后传递到服务器端的变量。

 &LT;%
昏暗serverVariable
请求通过HTTP GET来了。
serverVariable =的Request.QueryString(所以LocalVariable)
请求通过一个HTTP POST来了。
serverVariable =的Request.Form(所以LocalVariable)
通过要求无论是HTTP GET或HTTP POST来了
使用这个有它的开销。
serverVariable =请求(所以LocalVariable)
%GT;


相关链接

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?

解决方案

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

这篇关于在VBScript服务器标签内访问客户端变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆