In-depth explanation of Ajax in ArcGIS Server. Net ADF/callbackresult

Source: Internet
Author: User
In-depth explanation of Ajax in ArcGIS Server. Net ADF/callbackresult

Original article: http://bbs.esrichina-bj.cn/ESRI/thread-61303-1-5.html

The Forum already has many references to. net.
An excellent explanation of Ajax in the ADF, but the entry point is somewhat abstract. This article tries to focus on it from a beginner's point of view, starting with Ajax, to remove unnecessary technical details from the perspective of. net
Ajax has a clear understanding in the ADF. Supplemented by previous tutorials, we can fully master the working principle of Ajax in. Net ADF.

I often see a lot of friends in the forum asking: Why have I created a callbackresult, but the browser does not respond? Or I have put the ADF Control
In updatepanel, why can't I achieve what I want? 9.3. NET web
In the past two years, ESRI never said, "Sorry, there may be some problems with the Ajax workflow in our ADF." This proves that. NET web
The Ajax in the ADF is reliable enough. The fact is that the problem only occurs in our code. Even if you haven't figured out how to use Ajax in the ADF correctly, you can imitate others'
Code, hoping that you do not know why a callbackresult is created to work well for you. This is not what people who often deal with well-regulated machines do.
Situation.
Now that you have selected. net
If you want to make good use of the Ajax capabilities, it is necessary to figure out its principles. You can read them from the second part of this article. But maybe you are a ArcGIS
Server newbie, or even ASP. net newbie, you have to complete the GIS task in a short time, do not care about its principles, then you can read the first two parts of this article or only the second
. (If you are a beginner in web development, please learn HTML + CSS + JavaScript on your own)
Part 1: 1-minute Ajax time
Typical: ASP. NET
For Web pages earlier than 2.0, when the client sends a request, for example, click "Start processing, the class corresponding to the page will be submitted to the server for processing (the request sent is called
PostBack), after the page class is processed, the result (including the original page information) is output to the HTML + CSS + JavaScript that the browser can recognize and return to the client.
The result page is displayed. After the request is submitted, before the result page is displayed, your mouse will be in the shape of an hourglass, and the page cannot be operated. When the result page is displayed, you will see that the entire page is completely refreshed once. Emergence of AJAX
To solve this bad process, when you click the "Start processing" button on the page with Ajax skills, you can still operate other parts of the page, when the result is displayed, all pages are not refreshed.
But only the part that needs to be changed. At present, most pages on the Internet have Ajax capabilities, but do not be infatuated with Ajax. It is just a name of the effect. What you need to thank is
Two heroes for implementing Ajax in ASP. NET: client callback and ASP. NET appearing in ASP. NET 2.0
An extension of ASP. NET in 2.0
Ajax. But do not be infatuated with ASP. NET, because at the very bottom, the real hard work is the XMLHTTPRequest object in Javascript. The first two are for different degrees.
.
Finally, I will repeat the relationship between client callback and ASP. NET Ajax. You can use one of them as your Ajax Implementation Method Based on your needs.
Part 2: How to correctly use callbackresult
Anyone who wants to correctly use callbackresult should not refuse to explain what callbackresult is first.
There is only one map control and toolbar.
The entire page is not refreshed no matter how you operate the map or use the buttons and tools on the toolbar to interact with the map.
The control of the ADF has Ajax capability, because the same is true for other controls. How does the Server Control in the Web ADF achieve this? The client is undoubtedly used.
Callback (available in version 9.2 and later) or ASP. NET Ajax (available in version 9.3 and later ). How to use it? Don't worry about this problem. Let's dive deeper.
For Web
The ADF control, first look at a common operation: Zoom In the map with the mouse. The entire process is as follows: the user is on the map of the client (in fact, ESRI encapsulates the map
JavaScript Object, which belongs to the Web ADF
Javascript content) use the mouse to pull a box. When you release the mouse, the map object will send some information, such as the scope of the box, to the server, and the corresponding map control on the server.
The calculation is performed based on the current map range on the server side and the box range, and the new range is retained on the map control of the server. The server-side process is easy to complete, but these server-side controls also need
These results are synchronized to the client, otherwise the client will not see any changes. In this example, when the server synchronizes the result to the client, the map service outputs a new map image (if it is a cache Map Server)
To the client. As shown in:

Download(20.31 KB)


If you are not careful, you can directly look at the next section. If you are careful, you may ask why there are so many rounds of requests and responses during a zoom-in operation? The fact is that there are two requests in the map enlargement operation/
Response Process. Request 1: transfer the frame range to the server; response 1: return the expanded new MAP range to the client; (the map control of the client sends a response immediately after receiving the response 1) Request 2: based on the new Range
Map image to be returned by the map control on the server side; Response 2: transfer the map image to the client side. A amplification process is complete. Conclusion:
An operation by the ADF control may cause more than one request and response, but we only care about and only need to care about the initial request and the final result, because the Web
The ADF control is automatically completed for us.
Any operation on the Web ADF control is the same as the above process.
When a JavaScript Object sends a request, the corresponding control on the server processes the result and synchronizes the result to the client through a "response" (with a series of transparent subsequent requests and responses ).
"Response" is our main character: callbackresult object in the ADF (actually a JSON string ). It is a Web
The generation of the ADF control, the specific Web ADF of the Client
JavaScript Functions are responsible for parsing. No matter what the generated callbackresult looks like, the function name that the client is responsible for parsing is called
Do: processcallbackresult (). As shown in:

Download(32.33 KB)


For logic purposes, for example, an action corresponds to a callbackresult, so the callbackresultcollection is available. On the server
End, each web ADF
Control has a callbackresults attribute (callbackresultcollection). Why? Remember one thing:
"Who requests, who manages", that is, the control (the control corresponding to the server) that initiates the request (the control) is responsible for returning callbackresults.
After callbackresults is passed back to the client
The processcallbackresult () function in Javascript automatically parses the result. So what developers need to do is
The generated callbackresults is delivered to the control that is responsible for bringing back callbackresults-added to the callbackresults property of the control. You and
You do not need to worry about how callbackresults is resolved on the client, because it is automatically completed by processcallbackresult. Because the request can
Can be triggered by any control. Therefore, each WAF control has a callbackresults attribute.

For example, a new map service is added to the map control dynamically at runtime. In this process, the map control sends a request, so the map control on the server is responsible
Callbackresults is taken back to the client. To reflect this change, the TOC control in the page must be refreshed on the server side. refreshing this action will make the TOC control generate
Generate callbackresults. Remember "who requests, who manages", map requests, and map management. If you do not use the map
When callbackresults is brought back to the client, the TOC on the page will not change. The correct method is:

  1. ... <New map resource item added>
  2. Toc1.refresh ();
  3. Map1.callbackresults. copyfrom (toc1.callbackresults );

Copy code

The entire process is shown in:

Download(21.29 KB)

Not all changes to the Web ADF need to be handled manually. The following lists the internal relationships between some Web ADF controls:

  • When any command or tool is executed, the callbackresults of map is automatically copied to the Toolbar Control of map.
    To callbackresults In the toolbar. Therefore, if map is modified in the Custom command or tool
    Callbackresults is copied to the toolbar control that is responsible for bringing back the response;
  • TOC control bound to map, nodechecked event
    The callbackresults generated by map is automatically copied to the TOC control. When the map and TOC contain layers with visibility dependent on scale changes
    The scalechanged event copies the TOC callbackresults to the map control;
  • After scalebar is bound to map, scalebar's prerender event will copy scalebar callbackresults to the map control;
  • After the mapcopyrighttext control is bound to a map, the callbackresults of mapcopyrighttext is copied to the map control when map resource item is added or removed;
  • After magniier is bound to a map, when the display range of the map is changed or the tiling scheme is initialized, The callbackresults of magniier is copied to the map control;
  • The refresh () method of floatingpanel copies all the child controls callbackresults to their own callbackresults;
  • Taskresults
    After binding to map, executing a task will copy map callbackresults to the taskresults control.
    Callbackresults is then automatically copied to the executed task. So if you manually copy the callbackresults of map to
    If callbackresults is used, duplicate results will be generated;
  • The taskresults control has a predefined contextmenus. The itemclicked events of these right-click menu items automatically copy taskresults and map callbackresults to the contextmenu.

These internal relationships can be seen in the following list of web ADF controls. In addition to the preceding events, you need to add callbackresults to the control responsible for bringing back the response.

Download(169.51 KB)

In addition, you can customize callbackresult on the server side to modify non-web ADF controls on the client side or execute a javascript code. Custom callbackresult can be implemented through its constructor or static method, which is not described here:

Download(46.95 KB)

Of course, do not forget to copy the self-created callbackresults to the correct control.
So far, the question of how to correctly use callbackresult has been explained, all of which are called callback in the Web ADF.
Result Framework. It provides us with a unified programming experience, and you can choose whether to use client callback or ASP. NET Based on your own needs and programming experience.
Ajax. In the end, we strongly recommend that all web
The ADF control is far away from updatepanel. Because they can be registered to scriptmanager, and necessary content updates can be completed when they are completely refreshed. If you
Place the Web ADF control in updatepanel, and refresh the entire web ADF control even though it works properly.
Part 3: in-depth understanding of Ajax in Web ADF
Now let's take a look at the previous question: how does the Web ADF utilize client callback or ASP. NET in ASP. NET?
Ajax? First, it is clear that the Web ADF control can only use one asynchronous communication method, either client callback or ASP. NET.
Ajax. If the scriptmanager control exists on the page, it means that you have selected ASP. NET Ajax for asynchronous communication.
This method is also used in the actual operation of the ADF control. If the page does not contain the scriptmanager control, the Web ADF uses the client
Callback for asynchronous communication. The following describes the differences between the two methods in several scenarios:
If the client callback method is used:

  • Scenario 1: The Web ADF control is used to generate requests and responses, and the client processcallbackresult () is used to process responses.

 
This process is assumed in the second part in several scenarios.
When an ADF control is generated, the server processes it. You can also create a custom callbackresult to modify other controls on the page.
Callbackresults is assigned to the correct control. After the response is returned to the client
The processcallbackresult () function of JavaScript is parsed. :

Download(34.4 KB)

  • Scenario 2: Non-web ADF controls are used to generate requests and responses. The client processcallbackresult () is used to process responses.

 
For example, the icallbackeventhandler interface is implemented on the page.
When a button is sent, all the responses are returned in the raisecallbackevent () method, including non-Web
The structure of the callbackresult is constructed for the ADF control and centralized into the callbackresultcollection.
Callbackresultcollection. tostring () is returned in the getcallbackresult () method, which is processed by the client.
Specify the JS function of the callback response as the Web ADF.
The processcallbackresult () function in Javascript saves the trouble of parsing the callback response. :

Download(38.13 KB)

  • Scenario 3: Non-web ADF controls are used to generate requests and responses, and the client processes the responses on its own.


Besides using custom functions on the client to parse server responses including the callbackresult structure, the other functions are the same as scenario 2. In this case, you need to know
In the JSON string of callbackresult, the ":" separator is used to separate parameters, and the "^" separator is used to separate multiple callbackresults.
Processcallbackresult () can be defined in X: \ Inetpub \ wwwroot \ aspnet_client \ ESRI \ webadf
\ JavaScript \ ESRI. ADF. system. Debug. js. Unless you already have a set of JS frameworks for processing callback responses, it is recommended
The client uses processcallbackresult () for response processing. :

Download(47.53 KB)

If ASP. NET Ajax is used:

  • Scenario 1: WAF controls trigger partial PostBack

This scenario is similar to the client callback mode. The web ADF control that sends the request is a trigger in ASP. NET Ajax, and its callbackresults is registered as a data item of scriptmanager and returns the client. As shown in:

Download(38.41 KB)

  • Scenario 2: Partial PostBack triggered by a non-web ADF Control

Trigger is non-Web
Three methods are available to update the content of a Web ADF control: updatepanels, data items, or dynamic script.
Blocks. If you add the Web ADF control to updatepanel, the entire web
This is not necessary, web
The content to be updated by the ADF control is only the callbackresults generated by the server, which can be completed by the processcallbackresult () of the client,
It does not need to be replaced from start to end. Therefore, callbackresults can be used as data items or dynamic script.
Blocks to insert it to the response of partial PostBack.
As dynamic script blocks:Use Dynamic script
Blocks, the page must have at least one updatepanel (empty ). You only need to call
ESRI. ADF. system. processcallbackresult ()
To process the callbackresults generated by the server (as the parameter of this function), as follows:

  1. ...
  2. String jsprocesscallbackresult = string. Format ("ESRI. ADF. system. processcallbackresult ('{0 }');",
  3. Map1.callbackresults. tostring (). Replace ("\\","\\\\"));
  4. Scriptmanager. registerclientscriptblock (page, Sender. GetType (), "changeextent ",
  5. String. Format (jsprocesscallbackresult, map1.callbackresults), true );

Copy code

Advantages of this method: the Web ADF control does not need to be completely refreshed, and the client does not need to write the Response Parsing. Disadvantages: The registered dynamic script blocks will be saved in the client memory, the browser memory may eventually exceed the limit.
As data items:Data registered in scriptmanager
Items can be packaged on the server as a (JSON) string and uploaded to the client for processing. After the client receives the asynchronous PostBack response from the server
The pageloading event of the client is triggered before the modification. In this way, you can use pagerequestmanager to capture this event and register it as data
The callbackresults of items is handed over to processcallbackresult () for processing. The process is shown in:

Download(45.61 KB)

Register callbackresult as data item on the server:

  1. Scriptmanager1.registerdataitem (page, map1.callbackresults. tostring (), false );

Copy code

Listen to pageloading events:

  1. <SCRIPT>
  2. SYS. application. add_init (oninitfunction );
  3. // Called once during application Initialization
  4. Function oninitfunction ()
  5. {
  6. SYS. webforms. pagerequestmanager. getinstance (). add_pageloading (asyncresponsehandler );
  7. }
  8. // Called whenever a response to a partial PostBack is processed on the client
  9. Function asyncresponsehandler (sender, argS)
  10. {
  11. VaR dataitems = args. get_dataitems ();
  12. If (dataitems ['_ page']! = NULL)
  13. ESRI. ADF. system. processcallbackresult (dataitems ['_ page']);
  14. }
  15. </SCRIPT>

Copy code

Do you have any questions about Ajax in. Net ADF? If you do not hesitate, please continue to read the following learning resources.
S-H-G friends in the Forum series of tutorials:
Callbackresults of ArcGIS server9.3 Ajax series (I)
Client callback solution of ArcGIS server9.3 Ajax series (II)
ASP. NET Ajax solution of ArcGIS server9.3 Ajax series (III)
Summary of ArcGIS server9.3 Ajax series (4)
The jueery moderator's series of tutorials in the Forum:
Ajax (1) in ArcGIS Server. Net ADF (the post is lost, which can be found in the excellent collection of Community 3 anniversary)
Ajax in ArcGIS Server. Net ADF (2)
Ajax (II) in ArcGIS Server. Net ADF
Ajax in ArcGIS Server. Net ADF (3)
Details about client callback:
A newbie from carlbiao explained the callback mechanism of. net.
ASP. NET 2.0's client callback feature
About ASP. NET Ajax:
Ajax: the official Microsoft ASP. NET site
About XMLHttpRequest:
Http://www.w3school.com.cn/php/php_ajax_xmlhttprequest.asp
The last thing to do is to help the articles in the help of AGS, where all the details are:
Ajax and ASP. NET
Ajax capabilities of Web ADF controls Overview
Working with callbackresults
ASP. NET callback Solutions
ASP. NET Ajax partial PostBack Solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.