/*******************************************************************/
//translate by zcg
//
//Direction following
//Developing Applications
//Developing web application using the web adf
//web controls
//Ajax capabilities of web adf controls
//ASP.NET callback solution
/******************************************************************/
ASP.NET script callback solutions
ASP.NET 脚本回调解决方案
This document provides a detailed discussion on utilizing(利用) and extending AJAX capabilities of the Web ADF in an ASP.NET script callback solution. The content builds on information presented in a general discussion of AJAX and ASP.NET and an introduction to AJAX capabilities in the Web ADF . It is recommended that you are familiar with the technologies and concepts presented in both documents before proceeding.(程序,进行下去,进行)
这篇文档提供一个比较详细的关于利用和扩展WEB ADF 的AJAX的能力在ASP.NET 脚本回调解决方案上面的应用的讨论。这个讨论的内容是基于出现在有关AJAX and ASP.NET的讨论中和 AJAX capabilities in the Web ADF的简介中的相关信息。建议你在向下进行之前对出现在上面两篇文档里面的技术和概念都要熟悉。
Two properties of ASP.NET script callbacks are important to understand from the perspective(观点,视角) of Web ADF. First, the callback message for both the request and response can only contain string content. The format of the Web ADF callback message is serialized using JSON (JavaScript Object Notation
(记号,标记法)), a light-weight text-based data transmission(播放,发送) format(格式). The Web ADF defines a set of parameters to package information in the JSON formatted response message, explicitly designed to be processed by Web ADF JavaScript. Second, only one control on the server is responsible for processing a callback request and generating a response. Any ASP.NET server control can implement the System.Web.UI.ICallbackEventHandler interface and be utilized for processing callbacks (note that a Page is a type of server control). All Web ADF controls implement the ICallbackEventHandler interface and thus can process a callback request and generate a callback response(相应).
从Web ADF的观点出发,理解两个ASP.NET script callbacks重要的属性是非常重要的,第一:请求和相应的callback消息都只能包含字符串内容。
Web ADF回调消息的格式是连续的使用JSON,一个轻量级的文本数据发送格式。Web ADF 定义来一系列的变量使用JSON格式的响应消息来封装信息。尤其是那些被设计为由Web ADF脚本来处理的信息。第二:在服务器端只能有一个控件负责处理回调请求和生成相应的响应信息。任何一个ASP.NET控件都可以实现System.Web.UI.IcallbackEventHandler接口,并被利用处理回调(需要注意的是:一个页面也是一种服务端控件)。 所有的web adf 控件实现了System.Web.UI.IcallbackEventHandler 接口,因此他们能处理一个回调请求并生成一个回调的响应。
A callback request is initiated(开始) in a browser client with a call to the WebForm_DoCallback() function. The WebForm_DoCallback() function is included with JavaScript embedded in the ASP.NET System.Web.dll assembly and is available when a client script reference is registered (see the callback walkthrough in the AJAX and ASP.NET topic). How do you know which control is responsible for processing the callback? On the client, the first parameter in the WebForm_DoCallback() JavaScript function defines the id of the server side control to process the callback. In the server application, you can interrogate(审问,询问) the current Request object for the parameter "__CALLBACKID". The string returned is the id of a control in the page. The following code provides an example to determine if a postback is a callback, and which server-side control will process the callback:
一个回调请求是在客户端的浏览器里面开始,她向the WebForm_DoCallback()发送一个请求,webform _doCallBack()函数是和JavaScript一起嵌入在 ASP.NET System.Web.dll集合里面的,并且当一个客户端脚本接口注册后,她就可以获得了。那么,你是如何知道是那个控件是负责处理回调的呢?
在客户端,WebForm_DoCallback()函数的第一个参数就定义了在服务端处理请求的控件的ID。 在服务器端的应用中,你可以询问当前的请求对象的——CALLBACKID变量。 这个返回的字符串就是页面中的空间的ID。下面的代码提供一个例子来判断一个请求是否是一个回调并且由服务器端的那个控件来处理这个回调相应。
[C#]
if (Page.IsCallback)// Page .IsCallback当页面是第一次加载的时候,返回false ,当不是第一次加载而是第一次加载后的页面的刷新则返回true
{
string control_id = Request.Params["__CALLBACKID"];
//根据页面控件的ID,返回页面指定的控件
Control control = Page.FindControl(control_id);
}
Custom Callback Scenarios(剧本说明书,剧本)
There are three scenarios you may choose from to customize the Web ADF and utilize its implementation of the ASP.NET 2.0 Callback model. In all scenarios, the Web ADF CallbackResult and CallbackResultCollection classes are integral(基本的) to synchronizing changes to Web ADF controls on the server with the client browser.
这里有三个版本你可以选择来自定义web adf并利用他们已经实现了的ASP.NET 2.0回调模型。在所有的版本中,Web ADF CallbackResult and CallbackResultCollection 类是同步改变服务器端的web adf控件以及客户端浏览器的基本。
1) Use only Web ADF components to generate and process callbacks on the client and server
仅仅使用web adf组件在服务器和客户端产生和处理回调。
The diagram(图标) below illustrates how a Web ADF control on the client (rendered using HTML\JavaScript in the client browser) initiates the callback request, a Web ADF control processes the request on the server and generates one or more Web ADF CallbackResults. Then the client processes the callback response using Web ADF JavaScript designed for Web ADF CallbackResult strings. The most important characteristic of this scenario is that the Web ADF control is responsible for processing the callback and generating a response message in a format that Web ADF JavaScript can understand, namely the format defined by Web ADF CallbackResult classes and collections. Note that the Web ADF control creates the callback response for you - no explicit work is required on your part as the developer to construct or return the callback response.
下面的图表展示了一个客户端的web adf控件如何产生一个回调请求,一个web adf 控件在服务器端处理这个请求,然后生成一个或者多个web adf 回调请求结果。然后客户端使用为web adf回调结果字符串设计的web adfJavaScript类处理回调相应。这个版本最终的风格就是web adf 控件负责处理一个请求并生成一个web adf JavaScript可以理解的格式的响应消息,这个格式由web adf callbackresultclass and collection 来定义。既然web adf 控件已经唯一创建了回调响应,因此你作为一个开发者就没有特别指定的要去做的事情,比如:创建或者返回回调响应消息。
This is most common scenario for leveraging Web ADF AJAX capabilities - it includes the pattern for creating custom toolbar items that execute server-side logic using the Web ADF Toolbar architecture. When using the Web ADF Toolbar the ASP.NET callback framework is managed for you, so it's relatively easy to incorporate and execute custom code solutions in a Web ADF without explicitly managing callback messages or classes.
这是影响web adf ajax功能最常见的场景,它包括利用web adf 工具条风格创建自定义工具项并执行服务器端的逻辑的模型。 当使用web adf 工具条 ASP.NET回调框架就可以被用操作,这样就可以相对简单在web adf下执行自定义的客户端解决方法代码,而不用明确的去管理回调消息或者类。
This scenario also includes working with events on Web ADF controls that generate callbacks. The Web ADF control on which the event occurred is responsible for generating the callback response, but changes to other controls or browser content can be packaged with the response. The Web ADF control generating the callback response maintains a collection of CallbackResult objects (a CallbackResultCollection) accessible via the CallbackResults property. CallbackResults generated by other Web ADF controls can be added to the CallbackResults of the Web ADF generating the response. In addition, custom CallbackResult objects can be created and added to the collection.
这个场景当然也包含和事件一起工作的能够生成回调的web adf 控件。尽管产生event的web adf 控件负责生成回调响应,但是其他控件或浏览器内容的改变都可以和相应一起封装。 Web adf 控件生成的响应维持在一个callbackResult对象的集合里面,通过callbackresult属性是很容得到的。有其他web adf控件生成的callbackresults可以添加到web adf 的callbackresults里面生成相应。另外自定义callbackresult对象可以被创建并被添加到集合中。
2) Use a non-Web ADF component to generate and process the callback request and Web ADF JavaScript to process the callback response on the client
使用非 web adf 组件类生成和处理回调请求,由web adf JavaScript 在客户端处理回调请求。
The diagram below illustrates how a callback request can be generated by non-Web ADF content on the client and be processed by a non-Web ADF control on the server. Since Web ADF JavaScript is still responsible for processing the callback response, the response string needs to be in a format the processCallbackResult() function understands. The format is defined by the CallbackResult class. As a developer, this means you can use the CallbackResult and CallbackResultCollection class to construct the response string. The easiest way to accomplish this is to either create a CallbackResultsCollection or work with a Web ADF control's callback result collection and add CallbackResult objects to it.
下面的图标解释来如何由客户端的非web adf 内容如何生成一个回调请求,并由一个非web adf 控件在服务器端进行处理。因为web adf JavaScript是仍然负责回调响应,所以响应字符窜需要是processCallbackResult()函数能够理解的格式。这个格式是由callbackresult类来定义的。作为一个开发者,意味着你可以使用callbackresult和callbackresultcollection来操作响应字符串。完成这个操作最简单的方法是:创建一个callbackresultscollection或者和一个web adf 控件的callbackresult一起工作,然后将callbackresult对象添加到集合里面
Every Web ADF control maintains a collection of CallbackResult objects available via the CallbackResults property. For example, a simple HTML input button triggers(触发,因此) a JavaScript call to generate a callback request to the server specifying that the Page process the callback. The ICallbackEventHandler has been implemented on the Page, thus RaiseCallbackEvent() and GetCallbackResult() methods are called during request processing (page lifecycle). In the process, the Web ADF Map control is changed and a GridView of data needs to be rendered(提供) on the client. In the GetCallbackResult() method in the Page, a new CallbackResultCollection object is created and the CallbackResults from the Map control are copied to it (via the CopyFrom() method). A custom CallbackResult object is created to update the HTML content that represents the GridView on the server. The object is then added to the CallbackResultCollection (via the Add() method). The callback response string is returned from the GetCallbackResult() method by converting the CallbackResultCollection to a string (via the ToString() method). The Web ADF processCallbackResult() JavaScript function processes the callback results in the string and updates the appropriate content on the client. Using the diagram below to illustrate this example, the "Other Web control" represents the Page.
每一个web adf 控件对象都维持着一个callbackresult对象的结合,通过callbackresults属性很容易就可以获得。 比如: 一个简单的HTML按钮触发了一个JavaScript请求在服务器上产生来一个回调请求,指定服务器端的页面处理这个回调。 ICallbackEventHandler 函数在客户端页面中实现,因此RaiseCallbackEvent()函数以及GetCallbackResult()方法在(页面生命周期)处理请求的时候被调用。在处理的过程中,web adf 地图控件被改变并且在客户端的gridview控件需要提供数据。在页面的GetCallbackResult()方法里面,一个新的callbackresultcollection对象被创建并且来自地图控件的callbackresult(通过CopyFrom()方法)被拷贝到这里面。一个自定义的callbackresult对象被创建用服务器端的代表gridview的内容来更新HTML中的内容。对象然后被添加到CallbackResultCollection(通过Add()方法),callback 响应字符串从GetCallbackResult()方法中获取,这个字符串是由CallbackResultCollection转换而来的。Web ADF 的processCallbackResult()JavaScript函数处理回调结果中的字符串并更新客户端的属性内容。 使用下面的图表啦说明这个例子,其中Other Web control 代表页面。
This may be a common scenario for developers who want to leverage Web ADF JavaScript to process callback response content while triggering the callback request in a non-Web ADF control.
当触发一个非web adf控件的回调请求的时候,对于那些想通过影响web adf JavaScript处理回调请求内容的开发者来说,应该是一个非常熟悉的场景了。
3) Use non-Web ADF components to generate and process callbacks on the client and server
使用一个非Web ADF组件在客户端和服务器端产生和处理一个回调请求。
The diagram below illustrates the same behavior discussed in the previous scenario, except the callback response is processed by a non-Web ADF JavaScript function on the client. The difference is the developer is responsible for handling the raw callback response string on the client, which may contain CallbackResult formatted strings for Web ADF content. When a client script reference is generated on the server, the client-side JavaScript function to handle the callback response is defined. For example, the following line will generate a line of JavaScript that can be called to initiate a callback request on the client at runtime:
下面的表格说明了与上一章节讨论的同样的内容,除了回调响应是由客户端的一个非web ADF JavaScript功能处理。他们的区别是开发者服务在客户端来处理回调响应字符串,这个回调可能会包含web adf 内容的回调结果格式字符串。当一个客户端的脚本引用在服务器端产生的时候,客户端来处理回调响应的JavaScript功能函数就会被定义。比如:下面的代码在运行的时候就在客户端会产生一行可以被调用用来初始化一个回调请求的JavaScript代码。
[C#]
string m_ADFCallbackFunctionString =
Page.ClientScript.GetCallbackEventReference(this, "message", "customFunction", "context", "handleError", true);
The string returned from the GetCallbackEventReference method contains the call to the ASP.NET JavaScript function WebForm_DoCallback() with the provided parameters. The third parameter defines the JavaScript function to process the callback response string. In Scenario #2 the value is "processCallbackResult" - the Web ADF JavaScript function to process callback response strings in the format defined by CallbackResults. However, in this scenario the value is a developer-defined JavaScript function (in the example code, "customFunction"). If Web ADF CallbackResult formatted strings are returned to the custom function, the developer must parse(从语法上分析) the CallbackResult related content and forward it to the Web ADF's processCallbackResult() function. In general, a single CallbackResult uses the delimiter ":::" to separate parameters. Multiple CallbackResults are separated by "^^^". See the AJAX and ASP.NET topic for additional information on GetCallbackEventReference.
从GetCallbackEventReference()返回的字符串包含着带有提供的参数的ASP.NETJavaScript的WebForm_DoCallback()函数的请求。第三个参数定义了处理回调响应字符串的的JavaScript功能,尽管如此,在这个剧本里面,这个值是由开发者定义的的JavaScript功能(在例子代码里面:“customFunction”).
如果web adf callbackresult格式话后的字符串返回到这个自定义的功能,开发者就必须从语法上分析和callbackresult相关的内容,并且将他传递到web adf
processCallbackResult()函数。一般来说,一个单独的callbackResult使用“:::”来隔开参数。 多个callbackresult被”^^^”来分隔。更过的关于GetCallbackEventReference的内容请查看GetCallbackEventReference话题
This scenario is not common, but technically possible so it is presented here. The primary use-case for implementing this scenario is a business requirement to manually process the callback response string using custom JavaScript. If you have an existing JavaScript framework for processing callback response content and want to utilize Web ADF content, this scenario may provide a solution. Essentially(本质上) you will need to parse(从语法上分析) the Web ADF specific content in the response string and pass it to the Web ADF's processCallbackResult() JavaScript function.
这个剧本不是很常见,但是在技术上他是可行的,所以他出现在这里。实现这个剧本首要的一点是一个商业的要求,要求人工使用自定义JavaScript处理回调响应字符串。 如果你有一个已经存在的javascript框架来处理callback 响应内容,并且想利用web ADF内容,当前的剧本可能提供了一个解决方法。 实际上你应该需从语法上分析响应字符窜中web adf 特定的内容,然后将他们传递到web adf 的processCallbackResult ()JavaScript功能。
Walkthroughs
//总结
To interact with Web ADF controls on the client using callbacks, the Web ADF JavaScript libraries must be used. To process the callbacks on the server, you have two options: use the existing Web ADF Toolbar framework or implement your own solution to explicitly manage callback content. In addition, you may want to work with non-Web ADF content in your Web page. All three situations will be discussed in walkthroughs presented below:
//为来和客户端的web adf 控件进行交互,使用回调,Web ADF Javascript 类库就必须使用,为了在服务器端处理回调,你有两个选择:使用已经存在的web adf toolbar 控件或者实现你自己特定的管理callback内容的解决方法。 另外,你可能会想在你的页面和非web adf 内容一起工作。所有的这些情况都会在下面的教程里面讨论。
Walkthrough #1: Working with the Toolbar
// Walkthrough 1:使用Toolbar.
The following steps will walk you through adding a custom toolbar item to a Toolbar control in a Web ADF application. The custom tool will center the map on the user click.To start, create a Web application using the QuickStart Creating a Web application with the Web ADF controls as a guide. In Solution Explorer, right-click the Web project and select "Add New Item...". In the Add New Item dialog, under the Visual Studio Installed Templates section, select the "Class" item. Set the class file name to "CustomTool.cs" and make sure the language is "Visual C#". Visual Studio will prompt(提示) you to create an "App_Code" folder and place the new class file inside. Click Yes. The CustomTool.cs file should open for you to start adding content.
At the top of the CustomTool.cs file, add the following statements:
下面将会一步一步的指导你如何在一个web 应用程序中添加一个自定义的工具项到一个工具条控件里面。这个自定义的工具箱就会实现用户单击后地图距中显示。开始,根据Creating a Web application with the Web ADF controls中的指导创建一个web 应用程序。在解决方案管理器中右单击web 工程,在弹出的快捷菜单中选择”Add New Item…”。 在添加新项对话框里面,在VS安装的模板项部分,选择“calss”项,设置它的名字为:“CustomTool.cs”,并确保所使用的开发语言为C#,VS将会提示你创建一个名称为“App_Code”的文件夹,然后将我们新创建的类文件放置到里面,单击“YES”,customTool.cs文件就会自动打开并等待你为她添加内容。
在customTool.cs文件的顶部添加下面的声明:
[C#]
using ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
Remove the CustomTool constructor and implement the IMapServerToolAction interface on the CustomTool class. Add a ServerAction method to explicitly implement the interface. The code for the class should appear as follows:
//在customTool类里面删除customTool的构造函数并实现ImapServerToolAction接口,添加一个serverAction方法明确的实现这个接口。这个类的代码应该如下所示:
[C#]
public class CustomTool : IMapServerToolAction
{
public void ServerAction(ToolEventArgs args)
{
}
}
Within the ServerAction method, get a reference to the Map control from the ToolEventArgs.Control property.
在SeverAction的方法里面,从toolEventArgs的控件属性里面获取一个地图控件的引用
[C#]
Map map = (Map) args.Control;
Since the ClientAction for the tool will be set to "Point", cast the ToolEventArgs to PointEventArgs.
因为这个tool的ClientAction设置为“Point”,将toolEventArgs转换为PointEventArgs.
[C#]
PointEventArgs pargs = (PointEventArgs) args; //强制类型转换
Call the CenterAt method on the Map object and provide the screen point. This will change the extent of the map for the current session. Since the content of the map does not change, the map does not need to be refreshed. More specifically(特别的,明确的), changes in map scale or extent do not require a call to map.Refresh(). Changes to layer visibility or rendering or adding/removing content (e.g. graphics) require a call to map.Refresh().
调用地图对象的CenterAt方法并提供一个屏幕的坐标。 这个将会改变当前显示比例下的地图显示范围,因为地图的内容没有改变,因此地图是没有必要进行刷新的。 更确切的说,改变地图显示比例,或者地图的现实范围不需要调用地图的刷新函数。改变图层的可见性,或者执行一个添加/删除内容,需要调用地图的刷新函数。
[C#]
map.CenterAt(pargs.ScreenPoint);//调用的方法
The server-side code for the custom tool is finished. Now you need to add a tool item to the Toolbar control to trigger the action that executes the custom tool. Select the Toolbar control in design view or in the Properties window, then click the ellipsis for the ToolbarItems property. In the ToolbarItems Collection Editor dialog, add a new Tool item. In the Toolbar Items section, select the Tool item and click the Add button. The new Tool should appear under the Current Toolbar Items section.
Select the new Tool item and click the Show Properties button. A properties dialog for the new Tool should be displayed.
customTool的服务器端代码已经完成。 现在你就需要将一个toolitems项添加到toolbar控件来触发执行自定义tool的动作。在设计视图下选择toolbar控件或者在属性窗口,选择toolitem并单击添加按钮。那个新创建的tool就会出现在当前工具条的工具项的下面。选择新添加的tool 项,然后单击显示属性按钮,新添加的工具项的属性对话框就会出现.
Note that the EnablePostBack property is set to false by default. If false, using the tool at runtime will trigger an asynchronous(异步的) callback. If true, using the tool will trigger a synchronous postback. Keep the EnablePostBack property set to "False". Set the following properties:
需要注意的是:EnablePostBack属性默认情况下设置为false。如果为false,在运行的时候使用这个工具,将会 触发一个异步回调,如果为真的话,使用工具将会触发一个同步的ostback。 保持EnablePostBack 的属性为false。 设置下面的属性
Property |
Value |
Description |
Text |
CenterAt Tool |
Label for the tool in the Toolbar |
ClientAction |
Point |
Client event passed to the server |
ServerActionAssembly |
App_Code |
Class libraries associated with a Web site are compiled into an assembly named App_Code |
ServerActionClass |
CustomTool |
The name of the custom class which implements IMapServerToolAction and will be executed when this tool is used in the map |
Run the application. Use the "Center At" tool by activating the tool in the toolbar and clicking on the map. The initial mouse click will trigger a callback to execute the code in the CustomTool class. The custom tool causes the Map control to generate a callback string telling the client to retrieve the map and a new extent. The callback response to the initial mouse click is processed by the Web ADF JavaScript libraries and triggers one or more additional callbacks to retrieve new map images. Note that the Web ADF completely manages the callback messages for you. When finished, the map should center on the point clicked.
运行程序,使用工具条中的CenterAt工具并在地图上单击 。 鼠标单击事件开始触发一个回调事件来执行customTools类中的代码。Custom tool 触发地图控件产生一个回调字符串告诉客户端重新设置地图到一个新的显示范围。那个由鼠标单击引起的回调响应由web adf的JavaScript类库处理,触发一个或者多个额外的回调重新绘制地图图像。需要注意的是,web adf为你完成来回调消息的管理。 当完成后,地图就会以你单击的点为新的地图中心而显示。
The following diagram illustrates a Web ADF toolbar initiated callback event at runtime. The green circles indicate the sequence of events for the initial tool action. The orange circles track(踪迹)the subsequent callbacks initiated by the response to the tool action. A short description for each event is provided below:
下面的图表说明了一个web adf 工具项在运行的时候引起了一个回调事件。 绿色的圆圈指示了工具项动作事件发生的顺序。 黄色的圆圈追踪了tool的动作引起相应的回调踪迹的顺序。 每个事件的一个简单的介绍在下面都有提供。
Initial Callback (1-6):
1. After the Web page loads, the Center At tool is activated and the user clicks on the map. When the onClick event for the map is triggered, the MapMouseDown JavaScript function in the display_map.js resource is called. This function handles all mouse down events in the map. The postBack function in the display_dotnetadf.js resource consolidates(巩固、加强) the tool action details. The properties of the callback request include the name of the control to callback to on the server (the Map control) and the data associated with the user action (the click location in screen units).
web 页面加载完毕后,居中显示工具处于激活状态,用户使用该工具在地图上单击。 当地图的单击事件被触发后,display_map.js资源里面的MapMouseDown JavaScript 函数被调用。 这个函数处理地图上面的所有的鼠标单击事件。display_dotnetadf.js 资源里面的postBack函数加强了工具动作的细节。 Callback请求的属性包括向服务器发送请求的控件的名字(map控件)以及和用户动作相关的数据(在屏幕上单击的位置)
2. The postBack function sets the message and context variables to be sent to the server and calls the ASP.NET 2.0 defined WebForm_DoCallback function to create and send the callback request. The JavaScript function processCallbackResult, which is designed to process a Web ADF callback response, is registered when the call is made: WebForm_DoCallback('Map1',argument,processCallbackResult,context,postBackError,false)
postBack()函数设置了要发送到服务器端的消息和内容的变量。并且调用ASP.NET 2.0下定义的 WebForm_DoCallback函数创建并发送回调请求。处理回调结果JavaScript函数当一个请求创建后就被注册,这个函数被设计为处理一个web ADF回调请求,
WebForm_DoCallback('Map1',argument,processCallbackResult,context,postBackError,false)
A new HttpRequest object is created using the Microsoft.XMLHTTP component. This component will manage the asynchronous call to the server.
一个新的HTTPREQUEST对象利用微软的XMLHTTP组件技术被创建,这个组件将会管理服务器端的异步请求。
3. The Web ADF Map control implements the ICallbackEventHandler interface via its parent, the abstract(抽象的) WebControl class. When the callback request is received by the server, the Page initiates a limited callback version(版本) of the lifecycle. After Page Load, the callback request is handled by the Map's RaiseCallbackEvent() method, which merely stores the current event argument string. The bulk(主题) of the business logic for a callback in the Map control is present in the GetCallbackResult() method. Here the event argument is parsed(从语法上分析) to determine what type of action should be performed. Since this action involves a custom tool, the handlePostBack method calls the ServerAction method for the appropriate tool.
Web ADF 地图控件通过它的父亲实现了IcallbackEventHandler接口,一个抽象的webControl类。 当服务器端接收到回调请求后,页面引起了一个有限的回调生命周期。 当页面完成加载后,这个callback request就由地图控件的RaiseCallbackEvent()方法来处理,这个函数仅仅存储这个当前事件的参数字符串。 地图控件回调的商业逻辑主题出现在GetCallbackResult()方法中,。 事件的参数从语法上分析然后决定什么类型的动作应该被执行。 因为这个动作和一个自定义控件相关,handlePostBack()函数为tool调用专有的serverActions方法
4. The ServerAction method contains the custom code to modify(修改、更改) the Map control, depending on the user-defined action. The Map control generates one or more callback strings or CallbackResult objects to tell the client what to do to see the changes to the Map. The CallbackResult object contents consist of a simple string. In this example, the string will look something like:
"Map:::Map1:::shiftgrid:::1:::1:::66:::24"
ServerAction方法包含的用来更改地图控件的自定义代码是基于用户定义的动作。地图控件生成一个或者多个回调字符串或者callbackresult对象集合来告诉客户端要做什么来观看发生的地图控件上的变化。CallbackResult对象内容是由一个简单的字符串组成的。 在这个例子中,这个字符串看起来就像:
"Map:::Map1:::shiftgrid:::1:::1:::66:::24"
This information will be used by the client to update a control in the browser. The delimiter ":::" separates parameters for a client-side action on a specific control. Multiple actions, often on different controls, are separated by "^^^". Once the Map control generates the content of the callback response, the GetCallbackResult method passes this string back to the HttpRequest object waiting on the client.
这些信息将会被客户端使用来更新浏览器里面的控件。 通常发生在多个控件上面的多行为被“^^^”分割。 一旦地图控件生成了回调响应的内容,
GetCallbackResult()方法就会将这些字符串传递给在客户端等待的HttpRequest对象。
5. The ASP.NET 2.0 WebResource for callbacks directs the callback response string to the registered callback response function, in this case, the Web ADF defined processCallbackResult function. This function is designed to(皆在) parse the callback response for Web ADF controls on the client and trigger subsequent(随后的) actions to update client-side display.
ASP.NET 2.0WebResourc的回调指定来回调响应字符串注册的回调响应功能,在这种情况下,web adf 定义了processCallbackResult()函数的功能。这个功能皆在为web adf 在客户端从语法上分析callback响应,并且触发后面的动作更新客户端的显示。
6. The processCallbackResult function processes the callback response string and possibly, depending on the content of the callback, calls another JavaScript function. In this example, the callback response indicates that the "shiftgrid" function on the client-side Map object will be called (in the JavaScript file display_map.js). This marks the completion of the initial callback and the beginning of subsequent callbacks to update the client-side based on changes made in the initial callback.
processCallbackResult()函数处理来回调响应字符串和其的可能性,基于回调的内容,调用另外一个JavaScript函数。 在这个例子中,回调响应指示客户端map对象的shiftgrid函数将会被调用(这个函数在display_map.js中定义)。这个就标注着初步回调的完成以及随后的客户端更新的回调的开始,这些更新是基于由初步回调引起的改变上面。
Subsequent(随后的、后来的) Callbacks (A - C):
A. Processing changes made by an initial callback may take one or more subsequent callbacks to the server. In this example, the "shiftgrid" function is designed to(皆在) evaluate(评估,评价) the change in map extent and determine if new map images need to be generated by the server. It uses the other parameters in the callback response to determine which map regions (or tiles) needs to be retrieved, and if necessary, uses the ASP.NET 2.0 WebResource for callbacks directly. Since this example may request new content for a map tile, the updateTiles JavaScript function will process the callback response. The updateTiles function is associated with the callback when using the ASP.NET 2.0 WebResource:
WebForm_DoCallback('Map1',argument,updateTiles,context,postBackError,true)
由一个初步回调处理的改变可能会引起一个或者多个随后的回调发送到服务器端。在这个例子里面,“shiftgrid”皆在评估地图显示范围的改变,然后决定服务器端是否要产生一个新的地图图片。她在回调响应中使用其他的参数的决定地图的那个区域需要重新获取,如果需要的话,可以直接为回调使用ASP.NET 2.0WebResourcew。
因为这个例子可能会请求返回一个新的地图瓦片内容,updateTilesJavaScript功能将会处理这个响应。 当使用ASP.NET2.0WebResources的时候,updateTiles函数是和回调想关联的
B. Similar to the initial callback, the Page initiates a limited callback version of the lifecycle and the Map's GetCallbackResult() method processes the callback request. In this case, the event argument is "DrawTile" which triggers the server to create a map image for a grid (region) of the map. The grid, or tiling scheme, is generated by the Map control and shared with the Web ADF client libraries to manage map image retrieval more effectively. The callback response contains the map image as a bytestream or url, depending on Map control setting for blending, browser or Web-tier. The response is processed by the updateTiles function in display_map.js.
和初步回调类似,页面引起来一个有限的生命周期回调,地图控件的GetCallbackResult()方法处理这个回调请求。 在这种情况下,“DrawTile”就是事件的参数,她引起服务器为地图的一个区域创建一个新的地图图像,网格或者瓦片主题,是由地图控件生成的,和web ADF的客户端库分享来管理地图图像是比较高效的。回调响应以比特流或者URL来保存地图的图像,根据地图控件设置为混合,浏览器或者web 层。 响应是由display_map.js文件中定义的updateTiles()功能来处理的。
C. Elements in the client browser are updated to reflect the new content from the callback response. In this example, the callback response updates style sheet layers associated with the client-side rendering of the Map control.
客户端浏览器里面的元素将会更新来反映从回调响应中获取的新的内容。在这个例子里面,回调响应更新和客户端地图控件相关的的图层样式文件。
Walkthrough #2: Working with Web ADF controls using a custom callback
The following steps will walk you through adding an HTML button to a Web page. The button's onClick event will trigger an asynchronous(异步) callback to the server to change map extent, retrieve the callback string from the Map control, and return it to the client to trigger subsequent callbacks to update the map. You will add two input textboxes and a button, all client-side HTML elements. The textboxes will receive an x and y coordinate for the map to center on. The button will trigger the action via an asynchronous callback to the server. The Web ADF JavaScript libraries will be used to process the callback response.
下面的步骤将会带领你向一个web page里面添加一个HTML按钮控件,按钮的单击事件会触发并向服务器发送一个异步的回调请求来改变地图显示的范围。重新获取来自地图控件的回调字符串后,将它返回到客户端触发随后的回调来更新地图。你将会添加两个文本输入框和一个按钮,他们都是客户端的HTML元素。文本输入框将会获取一个X和Y坐标,用来设置地图的中心位置。 按钮将会通过一个发送到服务器端的异步回调触发这个动作(通过按钮将会触发一个异步回调到服务器端的动作)。 Web adf JavaScript 类库将会用来处理这个回调响应。
To start, create a Web application using the QuickStart Creating a Web application with the Web ADF controls as a guide or enhance the Web application created in Walkthrough
开始,我们根据Creating a Web application with the Web ADF controls的向导创建一个web 程序,或者增强一个在演练过程中创建的web程序。
#1.
Create the client-side code to trigger the callback first. Open the Default.aspx page in Design view. In the Visual Studio toolbox, expand the HTML tab and add two "Input (Text)" controls and an "Input (Button)" control. Optionally, you can also add labels for each textbox. The interface should appear as follows:
首先,创建客户端触发这个回调的代码。 在设计视图下打开defalt.aspx页面。 在VS的工具箱里面,展开THML页面,然后添加两个文本框和一个按钮控件。可选择的是,你可以每一个文本输入框添加一个label标签。 界面如下所示:
Open the Default.aspx in Source view. Assign an id value of "TextboxX" to the first textbox, an id value of "TextboxY" to the second textbox, and an onclick value of "ZoomToPointClick();" to the button. The HTML should look similar to the following:
在源视图模式下打开default.aspx页面,为第一个文本输入框控件设置一个ID为:“TextboxX”,为第二个文本输入框控件设置其ID为” TextboxY”。为按钮的onclick动做添加一个事件:“ZoomToPointClick()”。这个HTML源文件看起来应该如下所示:
<div style="left: 50px; position: absolute; top: 442px">X:</div>
<input type="text" id="TextBoxX" style=" 72px; left: 71px; position: absolute;top: 441px" />
<div style="left: 183px; position: absolute; top: 442px">Y:</div>
<input type="text" id="TextBoxY" style=" 72px; left: 203px; position: absolute;top: 440px" />
<input type="button" value="Zoom To Point" onclick="ZoomToPointClient();" style="left: 318px; position: absolute; top: 438px" />
Note the onclick event for the button references a JavaScript function, ZoomToPointClient(). Create this function near the top of the aspx page within the <head> tag or in the body of the page within a <div> tag. In both cases, the runat parameter must be set to "server" because the script variable <%= sCallBackFunctionInvocation %> is resolved at runtime . The function should retrieve the values from the textboxes and indicate the type of event. The event can be any valid string. You will use it on the server to determine how to process the request. In the following example, the event is "zoomtopoint".
<script language="javascript" type="text/javascript">
function ZoomToPointClient()
{
var x = document.getElementById('TextBoxX').value;
var y = document.getElementById('TextBoxY').value;
var message = 'zoomtopoint';
message += ',' + x + ',' + y;
var context = 'Map1';
<%=sCallBackFunctionInvocation%>
}
</script>
Now let's create the server-side code for the callback. First, set the sCallBackFunctionInvocation variable when the page loads. This variable is a string that represents the JavaScript method used by ASP.NET Web resources to send a callback to the server. As part of the ASP.NET Callback Manager implementation, the GetCallbackEventReference method will generate this string depending on a few input parameters. This method is discussed in greater detail above. In this implementation, we want the Web ADF JavaScript libraries to process the callback response, so the method's clientCallback argument is equal to the Web ADF JavaScript function that processes all initial callback responses. That function is named processCallbackResult.
现在让我们在服务器端为回调添加代码。 首先:在页面加载的时候设置sCallBackFunctionInvocation变量。 这个变量是一个字符串,表示javaScript方法被ASP.NET WEB 资源将一个回调发动到服务器。作为ASP.NET 回调管理实施的一部分,GetCallbackEventReference()方法会基于一些输入的变量,产生这个字符串。
这个方法在上面的讨论的非常详细。在这个实施中,我们想要web adf JavaScript类库处理这个回调响应,所以方法的客户回调参数的等于处理所有初步回调响应的web adf JavaScript 函数的参数。 这个功能命名为processCallbackResult();
[C#]
public string sCallBackFunctionInvocation;
protected void Page_Load(object sender, EventArgs e)
{
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this,
"message", "processCallbackResult", "context", "postBackError", true);
}
At runtime, the variable sCallBackFunctionInvocation is set to WebForm_DoCallback('__Page', message, processCallbackResult, context, postBackError, true) and rendered using a script variable in the aspx page, discussed in the previous step.
在运行的时候,sCallBackFunctionInvocation变量是在WebForm_DoCallback('__Page', message, processCallbackResult, context, postBackError, true)中设置的,并且如何在一个aspx页面中使用一个脚本变量已经在上一步讨论过了。
Next, we need to add code on the server to process the callback. Since the Page will be handling the callback request, it must implement the ICallbackEventHandler interface and define two methods: RaiseCallbackEvent and GetCallbackResult. RaiseCallbackEvent will receive the callback event message from the client. GetCallbackResult will send a callback response (string) back to the client after the server has finished processing the callback request.
接下来,我们需要在客户端添加代码来处理这个回调。 因为页面就会处理这个回调请求,因此他必须实现这个IcallbackEventHandler 接口并且定义两个方法:RaiseCallbackEvent和GetCallbackResult。RaiseCallbackEvent将会接受来自客户端的回调事件字符串。服务器在完成处理回调请求后,GetCallbackResult 将会发送一个回调响应(字符串)到客户端。
RaiseCallbackEvent will parse the callback event message to determine which method to call, then pass the message contents to that method. In this example, the callback message contains "zootopoint", the message contents will be sent to the ZoomToPointServer method. The ZoomToPointServer method will parse the callback message to get the user-defined point.
RaiseCallbackEvent 分析回调字符串来决定调用那个方法,然后将消息的内容传递到方法。 在这个例子中,回调消息包含“zoomtopoint”,这个消息将会被发送到ZoomToPointServer()。ZoomToPointServer()方法将会分析回调消息,获取用户定义的点。
The point will be used to construct an envelope and the map extent will be set to this envelope. When the map properties have changed, it will generate a callback string that the Web ADF JavaScript libraries can use to update the map display in the client. Like all Web ADF controls, the CallbackResults store the collection of callback strings to be used by the client. Simply calling ToString on the CallbackResults property will return all callback messages for the control as a string.
这个点将会用于构造一个信封,地图的显示范围将会发送到这个信封。 当地图的属性改变后,将会生成一个回调字符串,web ADF JavaScript 类库将会使用这个信封来更新地图在客户端显示的范围。 和其她的web ADF 控件一样,CallbackResults 存储着回调字符串的集合,调用 CallbackResults 的toString属性将回调消息以字符串的形式返回给所有的控件
Create a member variable, global to the Page class, as a private string to store the callback response. If the content of the Map control changes (e.g. layer visibility changed, layer added or removed) then the Map needs to be refreshed using Map.Refresh().
创建一个成员变量,是页面类中的一个全局变量,作为一个私有的字符串来保存回调响应。如果map控件的内容改变,哪儿map控件需要使用map.Refresh()函数来刷新地图控件。
[C#]
private string mapstring;
public void RaiseCallbackEvent(string eventArgs)
{
if (eventArgs.Contains("zoomtopoint"))
{
ZoomToPointServer(eventArgs);
}
}
public void ZoomToPointServer(string ea)
{
char[] parser_char = { ',' };
string[] messages = ea.Split(parser_char);
double map_width_eight = Map1.Extent.Width/8;
double x_center = Double.Parse(messages[1]);
double y_center = Double.Parse(messages[2]);
ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(x_center - map_width_eight, y_center - map_width_eight, x_center + map_width_eight, y_center + map_width_eight);
Map1.Extent = env;
mapstring = Map1.CallbackResults.ToString();
}
When the ZoomToPointServer method completes, the mapstring variable will contain a Web ADF specific string. For example:
"Map:::Map1:::changelevel:::-1:::9:::9:::5:::3:::0:::0:::444:::304:::-1"
当ZoomToPointServer()方法执行完毕后,mapstring变量就会包含一个web adf 指定的字符串,比如:
"Map:::Map1:::changelevel:::-1:::9:::9:::5:::3:::0:::0:::444:::304:::-1"
The Web ADF JavaScript libraries are designed to parse and handle this string.
At the end of the callback version of the page lifecycle, the GetCallbackResult method is called to return a string to the client. In this example, GetCallbackResult merely returns the mapstring variable set in the ZoomToPointServer method.
web adf JavaScript类库被用来分析和处理这个字符串。在页面的生命收周期回调的结束时,GetCallbackResult()函数被调用返回一个字符串到客户端。 在这个例子中,GetCallbackResult()方法仅仅返回在ZoomToPointServer()方法中设置的
Mapstring变量。
[C#]
public string GetCallbackResult()
{
return mapstring;
}
Recall when the GetCallbackEventReference method was used to create the client-side JavaScript call to initiate the callback. In this example, the clientCallback argument was set to processCallbackResult, a Web ADF managed client-side JavaScript function. As a result, when the client receives the callback response, the ASP.NET Callback Manager knows to send the response to processCallbackResult function. The function parses the response and updates the client session. In many cases, depending on the controls that need to be updated in the page, a series of additional callbacks are generated and processed.
回想一下,在什么时候GetCallbackEventReference()被用来创建客户端的JavaScript 调用开始一个回调。 在这个例子中,clientCallback 变量被设置为processCallbackResult。 一个web adf 管理客户端的JavaScript 功能。 结果是,当客户端接收到回调响应,ASP.NET Callback 管理器知道将响应发送到processCallbackResult()方法。这个方法分析响应并更新客户端的会话。在很多情况下,基于页面中需要更新的控件,一系列额外的回调被生成并被处理。
Run the application. Enter a point (a valid location for the map) and click the ZoomToPoint button. The map should change extent and zoom to the point specified.
运行程序,输入一个点(地图上的一个可以获取的的位置),单击ZoomToPoint按钮,地图将会改变显示的范围并缩放到指定的点。
The following diagram illustrates a custom callback event that works with the Web ADF controls at runtime. The green circles indicate the sequence of events for the initial action. The orange circles track the subsequent callbacks initiated by the response to the action. A short description for each event is provided below:
下面的图标说明了一个在运行的时候与web adf控件一起工作的自定义回调事件。
绿色的圆圈指示了事件序列初步的动作。黄色的圆圈跟踪了由响应引起的回调到动作的顺序。针对每个事件在下面都有一个简短的说明
Initial Callback (1-5):
1. After the Web page loads, an x and y value for a point are entered in the appropriate text boxes. The onClick event for the ZoomToPoint HTML button calls the ZoomToPointClient JavaScript function.
页面完成加载后,一个点的X和y值被输入到文本框里面,ZoomToPoint HTML按钮的onClick()事件调用ZoomToPointClient()JavaScript函数。
2. The ZoomToPointClient function gets the values of each textbox containing the user-defined point, sets the custom message and context parameters for the callback. All are used by the ASP.NET 2.0 WebForm_DoCallback function to initiate a callback. The line containing this function is generated during page load by a call to the Page.GetCallbackEventReference method.
ZoomToPointClient()函数获取每一个文本框中包含用户定义的点的值,为回调设置自定消息和内容变量。所有的这些变量都被ASP.NET 2.0 WebForm_DoCallback方法使用初步一个调用。包含这个函数的代码行在页面加载的过程中通过调用
Page.GetCallbackEventReference方法生成。
The first parameter uses the page id to define that the Page object will receive the callback request. The Page must implement the ICallbackEventHandler interface. The message is a custom string containing the type of server action (zoom to point) and the x and y values. The callback response will be processed by the Web ADF JavaScript function processCallbackResult, which is designed to process a Web ADF callback content. The context may optionally(可供选择的) be used by the client to reference the callback request. The JavaScript function will appear as follows :
WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true)
第一个变量使用页面的ID来定义那个页面对象将会接收回调响应。 页面必须实现IcallbackEventHandler接口,这个消息是一个自定义字符串,她包含服务器端动作的种类,和X,Y的值。回调响应将会被web adf 的JavaScript函数processCallbackResult处理,processCallbackResult就是被定义为处理web adf 回调内容的。客户端可以有选择的使用上下文来引用回请求。 这个JavaScript函数看起来如下所示:
WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true)
A new HttpRequest object is created using the Microsoft.XMLHTTP component. This component will manage the asynchronous call to the server.
使用Microsoft.XMLHTTP组件创建一个新的HttpRequest对象,这个组件将会管理发送到服务器端的异步调用。
3. The Page implements the ICallbackEventHandler interface. When the callback request is received by the server, the Page initiates a limited callback version of the lifecycle. After Page Load, the callback request is handled by the Page's RaiseCallbackEvent() method, which checks the message for an action. If it finds "zoomtopoint", the ZoomToPointServer method is called. The ZoomToPointServer method parses the message to find the x and y coordinates, uses them to define a new extent, then changes the extent on the Map control. When the map extent changes, the Map control generates a string that can be used by Web ADF JavaScript to update the map in the browser. To get this string, use the CallbackResults property on the Map and convert it to a string using ToString(). In this example, the string will look something like:
"Map:::Map1:::changelevel:::1:::1:::66:::24"
页面实现来IcallbackEventHandler 接口。 当服务器端接收到回调请求后,页面引起了一个有限的页面生命收起版本的回调。页面加载后,回调请求被页面的RaiseCallbackEvent()方法处理,RaiseCallbackEvent()方法为这个动作检查这个消息。 如果他找到了“zoomtopoint”字符串,zoomtoPointSever方法就会被调用,RaiseCallbackEvent()方法分析消息找到x,y坐标值,使用他们定义一个新的范围,然后改变地图控件的现实范围。 当地图控件显示范围改变后,地图控件就生成一个字符串,web adf JavaScript 使用这个字符串来更新浏览器里面的地图控件。为来得到这个字符串,使用地图控件的CallbackResults属性,并使用ToString()方法将它转换为一个字符串。在这个例子里面,他们看起来如下所示:
"Map:::Map1:::changelevel:::1:::1:::66:::24"
This information will be used by the client browser to update the Map control in the browser. The delimiter(定界符,分隔符) ":::" separates parameters for a client-side action on a specific control. Multiple actions, often on different controls, are separated by "^^^". Return this string via the return value from GetCallbackResult() method.
这些信息将会被客户端的浏览器使用,用于更新浏览器里面的地图控件。分隔符“:::”在指定的控件上面为客户端的动作分割参数。 多动作的由“^^^”分割,这种情况常发生在不同的控件上面, 通过GetCallbackResult()方法返回的值来返回这个字符串
4. The ASP.NET 2.0 WebResource for callbacks directs the callback response string to the registered callback response function, in this case, the Web ADF defined processCallbackResult function. This function is designed to parse the callback response for Web ADF controls on the client and trigger subsequent actions to update client-side display.
ASP.NET 2.0 WebResource 的回调指导回调响应字符串注册到回调响应的功能,在这种情况下,web adf 定义来processCallbackResult 功能。 这个功能被定义为分析在客户端的web adf 控件的回调响应,然后触发随后的动作来更新客户端的现实。
5. The processCallbackResult function processes the callback response string and possibly, depending on the content of the callback, calls another JavaScript function. In this example, the callback response indicates(指示,指出) that the "setUpLevelGrid" function on the client-side Map object will be called (in the JavaScript file display_map.js). This marks the completion of the initial callback and the beginning of subsequent callbacks to update the client-side based on changes made in the initial callback.
processCallbackResult()函数处理回调响应字符串以及其他的可能,基于回调的内容,调用另外的JavaScript功能。 在这个例子里面,回调响应指出“setUpLevelGrid”函数由客户端的的地图对象调用(函数定义在display_map.js),这标志着初步回调的的结束,以及随后的客户端更新回调的开始,这些更新是基于在初步回调中生成的变化。
Subsequent(随后的,后来的) Callbacks (A - C):
随后的回调。
A. Processing changes made by an initial callback may take one or more subsequent callbacks to(到) the server. In this example, the "setUpLevelGrid" function is designed to evaluate the change in map extent and determine if new map images need to be generated by the server. It uses the other parameters in the callback response to determine which map regions (or tiles) needs to be retrieved, and if necessary, uses the ASP.NET 2.0 WebResource for callbacks directly. Since this example may request new content for a map tile, the updateTiles JavaScript function will process the callback response. The updateTiles function is associated with the callback when using the ASP.NET 2.0 WebResource:
WebForm_DoCallback('Map1',argument,updateTiles,context,postBackError,true)
A:处理由初步回调生成的变化可能需要一个或者多个随后的回调到服务器端。 在这个例子中,“setUpLevelGrid”函数被设计为评估地图显示范围的改变并决定服务器端是否有必要生成新的地图图像。 他们在回调响应中使用其他的参数来确定地图的那个区域(或者瓦片需要更新)。并且如何需要的话,直接使用ASP.NET 2.0 WebResource 回调。因此这个例子也许为地图瓦片请求信的地图内容,updateTiles JavaScript 函数将会处理那个回调请求。 当使用ASP.NET 2.0 webresuorce的时候,updataTiles功能是和回调函数相关的。
WebForm_DoCallback('Map1',argument,updateTiles,context,postBackError,true)
B. Similar to the initial callback, the Page initiates a limited callback version of the lifecycle and the Map's GetCallbackResult() method processes the callback request. In this case, the event argument is "DrawTile" which triggers the server to create a map image for a grid (region) of the map. The grid(栅格), or tiling scheme(计划、方案), is generated by the Map control and shared with the Web ADF client libraries to manage map image retrieval more effectively. The callback response contains the map image as a bytestream or url, depending on the Map control setting for blending, browser or Web-tier. The response is processed by the updateTiles function in display_map.js.
和初步回调相似的是,页面引起来一个有限回调版本的生命周期,地图控件
GetCallbackResult()方法处理回调请求。 在这种情况下,事件的参数是:DrawTile,这个参数触发服务器为地图的一个瓦片创建一个地图图像。 这个栅格或者瓦片方案是由地图控件生成的,并且和web adf 客户端的库共享,更高效的管理地图图像。
回调响应以比特流或者URL的方式包含着地图图像,基于设置为混合的地图控件,浏览器或者web 层。 这个响应由在display_map.js文件中定义的updateTiles方法来处理。
C. Elements in the client browser are updated to reflect the new content from the callback response. In this example, the callback response updates style sheet layers associated with the client-side rendering of the Map control.
客户端浏览器里面的元素被更新,用来反映回调响应中的新内容。在这个例子中,回调响应更新了与客户端地图控件相关的图层样式表
Walkthrough #3:
Using the Web ADF callback framework to interact with non-Web ADF elements
The following steps will walk you through extending Walkthrough #1 or #2 to update text in four <div> tags in the same Web page as the Map control. Each tag will represent the north, south, east, and west extent of the map. Note that the div tags only reside on the client, in the browser.
下面的讲解中将让你一步一步的扩展Walkthrough #1 or #2,以及四个在div标签中的文本框,以及同样的页面,同一个地图控件。每一个标签分别代表地图范围的北,南,东,和西。 需要注意的是div标签仅仅出现在客户端,在浏览器里面。
When the map extent changes, the text within each div will be updated to represent the current extent parameters. A custom CallbackResult object for each div tag will be created and appended to the callback result for the Map control. The Web ADF JavaScript function processCallbackResult() contains logic to process non-Web ADF content depending on the parameters provided.
当地图显示范围改变后,每一个div标签里面的文本框就会更新,代表当前地图显示范围的参数。 每一个div标签都要创建一个自定义的callbackresult对象,为map控件附加到回调结果集合。 Wen adf JavaScript 函数processCallbackResult()包含基于提供的参数处理非 web adf 内容的逻辑。
The parameters are listed and discussed in the custom callback events section above. The same function flow will occur as specified in the previous walkthroughs, we will merely append additional content on the initial callback response to update non-Web ADF elements in the Web page.
这些参数在上面的custom callback events 里面列出并讨论。 和上面的工作步骤中指定的功能将会出现。我们紧紧是扩展初步回调响应中的添加的部分,在web 页面中更新非web adf的元素。
Start with by completing walkthrough #1 or #2, then add the content in this walkthrough.
In the aspx page, add four div tags, one on each side of the Map control. Note, these are simple HTML elements.
以已经完成的演练为基础,然后在这个演练里面添加内容。 在aspx页面中,添加4个div标签,在地图的周边一边一个。 注意:他们紧紧是简单的HTML元素。
<div id="LabelW" style="left: 13px; position: absolute; top: 259px; 50px;"></div>
<div id="LabelS" style="left: 262px; position: absolute; top: 436px; 50px;"></div>
<div id="LabelE" style="left: 533px; position: absolute; top: 259px; 50px;"></div>
<div id="LabelN" style="left: 262px; position: absolute; top: 87px; 50px;"></div>
The Web page should appear as follows. Note that(请注意:) no additional JavaScript code is required by the browser:
页面应该如下图所示,请注意,浏览器端不需要两外添加JavaScript代码。
Add a new event handler for an ExtentChanged event on the Map control. In this example, the name of the method is Map1_ExtentChanged.
为地图控件显示范围改变时间添加一个新的事件处理函数。 在这个例子里面:方法的名称为:Map1_ExtentChanged();
[C#]
protected void Map1_ExtentChanged(object sender, ExtentEventArgs extentEventArgs)
{
Get the new map extent via the ExtentEventArgs parameter and create a sorted list to store the north, east, south, and west values.
[C#]
ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope = extentEventArgs.NewExtent;
SortedList<string, string> extentList = new SortedList<string, string>();
extentList.Add("n", adfEnvelope.YMax.ToString("N"));
extentList.Add("e", adfEnvelope.XMax.ToString("N"));
extentList.Add("s", adfEnvelope.YMin.ToString("N"));
extentList.Add("w", adfEnvelope.XMin.ToString("N"));
Iterate(反复去做) through the sorted list to construct a new CallbackResult object for each client-side element that needs to be changed. In this case, each HTML element is a type of "div" and has a unique id. We are going to change the HTML content (or inner content) within the div tag. Four parameters will be used to construct the CallbackResult.
反复的遍历sorted list ,为每一个需要改变的客户端要素构造一个新的callbackResult对象。 在这种情况下,每个HTML元素都是一类含有唯一ID的div标签。 我们将要改变在DIV标签里面的HTML内容。 四个变量将会被使用来构建CallbackResult。
The first parameter is a string representing the type of html element, the second represents the unique id of the element. The third parameter is one of four actions: "content", "innercontent", "image", and "javascript". The fourth and final parameter is the value or content on which a client-side action will take place. Its use is determined by the action. In this case, we will change the content within the div tag, so we choose "innercontent". The final parameter is the string value that will be used to change the div tag content.
第一个参数是一个字符串代表HTML元素的类型,第二个参数代表元素唯一的ID值, 第三个元素是四个动作中的一个; "content", "innercontent", "image", and "javascript". 第四个和最后一个参数是一个值或者内容,他们代表客户端的动作将会在什么上面发生。 她的使用取决于行动。在这样的情况下,我们将改变div标签里面的内容,所以我们选择innercontent. 最后的一个参数是一个字符串值,在改变div标签内容的时候将会使用这个值。
[C#]
foreach (KeyValuePair<string, string> key in extentList)
{
string value = key.Value.ToString();
CallbackResult callbackResult = new CallbackResult("div",
"Label" + key.Key.ToString(), "innercontent", value);
Once the CallbackResult objects are created, they need to be added to the control responsible for sending a callback response string to the client browser. In this case, the string returned from the Map controls GetCallbackResult() method will be returned to the client. As a result, we can add each CallbackResult object to the Map.CallbackResults collection. Since layer visibility and rendering did not change, the Map control does not need to be explicitly refreshed.
一旦CallbackResult 对象被创建,他们将会被添加到一个负责发送回调响应字符串到客户端浏览器里面的控件。 这样,从地图控件的GetCallbackResult()方法中获取的字符串将会被返回到客户端。 结果,我们可以添加任何的callbackresult对象到地图控件的callbackreslts集合里面。 因此地图图层的可见性没有改变,因此地图控件不需要特别的指出要进行刷新。
[C#]
Map1.CallbackResults.Add(callbackResult);
}
}
The callback result string will look something like:
"Map:::Map1:::changelevel:::-1:::36:::37:::10:::12:::0:::0:::444:::304:::-1:::0:::0^^^
div:::Labele:::innercontent:::-70.74^^^div:::Labeln:::innercontent:::43.78^^^
div:::Labels:::innercontent:::36.94^^^div:::Labelw:::innercontent:::-80.74"
At runtime, the callback string generated by the custom CallbackResults will enable the modification of non-Web ADF controls and elements. Upon the initial map extent change, the div tag content around the map will be updated dynamically in the browser, without a full page postback. The image below shows a runtime example of the Web ADF application built in this scenario.
在运行的状态下,由自定义的CallbackResults 函数产生的回调字符串能够重新定义非WEB adf 控件和元素。地图返回变化后,map控件周围的的div标签的内容就会在同步在浏览器里面更新,而不用一个完整的页面回发。 下面的图片显示了一个运行状态下的基于这个章节创建的web adf 应用程序的例子,