Callback implementation mechanism in ASP. NET2.0

Source: Internet
Author: User

Callback implementation mechanism in ASP. NET2.0

Many of my friends recently wrote articles about using callback, which is also a bit tricky. They also used graffiti to explore the potential of callback. The general usage of callback is simple. It is sufficient to directly refer to the help and example of msdn. However, if you really want to make good use of, use excellent, or develop some WEB Components Based on the callback mechanism, you must first have a deep understanding of the callback implementation mechanism. In this article, Teddy will work with you to parse the entire call and feedback mechanism of callback. We believe it will be helpful to help you better use callback.

Callback vs Atlas

First, let's talk about Atlas. Many may find it strange that with Callback, why does Atlas come out again? How does Atlas author explain this problem? I did not investigate it. However, from my personal feelings about callback and atlas, I feel that callback is a very similar implementation as postback, so that users can use it similarly. However, it is similar to the postback mechanism. It should be said that it is not very convenient to use and is not easy to expand. Of course, this is compared with the implementation of other AJAX frameworks. Therefore, Microsoft draws on many existing AJAX implementations, such as Prototype, Backbase, and AJAX. NET. Combined with some special functions of ASP. NET2.0, it has developed such a well-known AJAX framework. How nice AJAX applications are to be developed based on Atlas, which is difficult to quantify, but at least not worse than other AJAX frameworks. In addition, the background of Microsoft, and the application promotion of heavyweight sites such as live.com, the impact of which is certainly worth looking forward.

However, this does not mean that the callback implementation is useless. As a programmer, we need to have a correct attitude and use the most correct technology in the correct use case. No framework is omnipotent and suitable for any use environment. Just as everyone is arguing about the best software development method, such as cmme, RUP, XP, AGILE ~~, In fact, there is no best, and the most suitable is the best. What we should do most is to understand the principles, advantages and disadvantages of various solutions, so as to reasonably use the correct tools to solve practical problems.

Begin from Client Script

We all know that AJAX has two implementation mechanisms at the underlying layer: XMLHTTP and IFRAME. Before the word "AJAX" was widely used, the functional frameworks based on the two underlying implementations or the implementation of the refreshing effects based on the two technologies were widely used. Of course, since today, in terms of using interfaces, the details of these underlying mechanisms are often hidden by the framework and the use of interfaces becomes increasingly simple. Users only need to call these simple interfaces, there is no need to know how to achieve the effect.

However, since we want to parse the callback implementation mechanism, let's start with a client script call called by callback to see how Microsoft implemented this callback mechanism.

1. ClientScript. GetCallbackEventReference (...)

To activate a callback, you must first make a call in the client. A typical Call syntax is as follows:

 
 
  1. <script language="javascript" type="text/javascript"> 
  2. function any_script_function(arg, context)  
  3. {  
  4. <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context")%>;      
  5. }  
  6. </script> 

ClientScript. GetCallbackEventReference (...) returns the actual callback script based on the input parameters. This function has multiple overloaded versions. Therefore, you can refer to MSDN for the meanings of these parameters. Take the parameters in the above sample code as an example:

◆ This indicates that the server control that executes the callback is the current Page. The current Page must implement the ICallbackEventHandler interface, including the two interface functions: string GetCallbackResult () and void RaiseCallbackEvent (eventArgument, this parameter can also be a reference to a WEB control. Of course, this space must also implement the ICallbackEventHandler interface;

◆ "Arg" is the value of eventArgument, a parameter that will be passed to RaiseCallbackEvent. It can be a string in a custom format;

◆ "Eseserverdata" is the name of the client script function that processes the returned content after the callback is successful. This function must exist on the page where the callback is executed and can contain two parameters, for example:

 
 
  1. <script type="text/javascript">   
  2. function ReceiveServerData(result, context)   
  3. {   
  4.            
  5. }   
  6. </script> 

These two parameters are the result of the callback returned data, and the context parameter when the callback is triggered when the response is not fixed. Of course, these two parameters are of the string type.

◆ "Context" does not need to be explained. Remember that this parameter will be passed to the specified data processing function. According to the official documentation of MSDN, context is generally used to transmit the script code that needs to be called in the return data processing function of the client. However, you can upload everything, it can be viewed as a parameter transfer channel between the client callback initiator and the receiving segment that processes the returned data.

2. WebForm_DoCallback (...)

OK. After understanding the meaning of the above Code, let's take a look at the previous "<% = ClientScript. getCallbackEventReference (this, "arg", "ReceiveServerData", "context") %>; "What will it look like during runtime? We only need to check the page source code when the page is running. In fact, the server generates the following script code for us:

 
 
  1. <scriptlanguagescriptlanguage="javascript"type="text/javascript"> 
  2. functionany_script_function()  
  3. {  
  4. WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false);  
  5. }  
  6. </script> 

What does this code mean? Obviously, he calls a system and defined script Function: WebForm_DoCallback. We need to find out what this function has done for us. In the page source code at runtime, we can easily find the source of this script. We noticed a script, src = "/TestCallbackWeb/WebResource. axd? D = HEcYmh-7_szSIu1D_mHSEw2 & amp; t = 632661779991718750 ", which defines WebForm_DoCallback. Let's download it with flashget and change the extension to. js. Let's look at the source code. It is not obfuscated, so it is easy to understand. The callback implementation mechanism in ASP. NET2.0 is described above.

  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

Related Article

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.