callback mechanism in depth analysis of ASP.net 2.0

Source: Internet
Author: User

The general use of callback is simple enough to refer directly to MSDN's help and examples. But want to really use good, use fine, or want to develop some based on callback mechanism of Web components, then, must first in-depth understanding of callback implementation mechanism. In this article, Teddy will work with you to resolve the callback of the entire call, feedback mechanism, I believe that to help you better use of callback, will have some benefits.

Callback vs Atlas

First, let's talk about Atlas. Many friends may find it strange that there are already callback, why Atlas? On this question, how does the atlas author explain, I did not investigate. Just from my personal experience with callback and Atlas, I feel that callback as a very similar implementation of interface and postback is definitely designed to allow users to use it like postback. However, it is a similar postback mechanism, it should be said to use is not particularly convenient, and not easy to expand, of course, this is compared to other AJAX framework implementations. Therefore, Microsoft has borrowed many of the existing AJAX implementations, such as Prototype,backbase and Ajax.NET, and combined with some of asp.net2.0 's unique features, invented such a absorbing AJAX framework. How nice it is to develop AJAX applications based on Atlas, it's hard to quantify, but at least not as good as the rest of these Ajax frameworks, and the impact of Microsoft's background, and the application of a heavyweight site like Live.com, is certainly worth looking forward to.

However, this is not to say that callback implementation is not useless, as programmers, we need to have the right attitude, in the correct use of the situation, the use of the most correct technology. No framework is omnipotent, it is suitable for any use of the environment; like everyone is arguing about the software development method best, Cmmi,rup, xp,agile~~, in fact, not the best, the most suitable is the best. What we should do most is to understand the principles and advantages and disadvantages of various schemes, so that we can use the correct tools to solve practical problems.

Begin from Client Script

As we all know, all Ajax, from the bottom up, there are two kinds of implementation mechanisms: XMLHTTP and IFRAME. Before the word Ajax became a widespread concern, a functional framework based on the two underlying implementations, or a no-refresh effect based on both technologies, was widely used. Of course, the development to today, in the use of interfaces, the details of these low-level mechanisms are often hidden by the framework, the use of interfaces becomes more and more simple, users simply call these simple interfaces, there is no need to know how to achieve the specific effect.

However, since we are trying to parse the implementation mechanism of callback, let's start with a callback called client script call to see how Microsoft realizes this callback mechanism.

1, clientscript.getcallbackeventreference (...)

To fire a callback, first of course you need to issue a call in the client-side. A typical invocation syntax is as follows:

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

Clientscript.getcallbackeventreference (...) The actual callback script is returned based on the parameters passed in. This function has multiple overloaded versions, so you can refer to MSDN for the meaning of these parameters. For the parameters in the example code above:

-This indicates that the server-side control that executes the callback is the current page, and the current page must implement the ICallbackEventHandler interface, including the need to implement string GetCallbackResult () and void RaiseCallbackEvent (eventargument) These two interface functions, 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 the parameter eventargument that will be passed to the raisecallbackevent, allowing the person to be a custom formatted string;

-"Receiveserverdata" is the name of the client script function that handles the returned content after the callback succeeds, and the function must exist on the page that executes the callback, and the function can contain two parameters, such as:

<script type="text/javascript">
function ReceiveServerData(result, context)
{}
</script>

These two parameters, respectively, are the return data result of the callback, and the context parameter that was returned intact when we fired the callback, of course, both of which are string types.

-"context" does not need to explain, remember that this parameter will be passed to the specified return data processing function on the line. The official documentation for MSDN says the context can generally be used to pass the scripting code that needs to be invoked in the client's return data processing function, but in fact, whatever you preach, see it as a parameter-passing channel between the triggering end of the client callback and the receiving segment that handles the return.

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.