The general use of callback is relatively simple. It is enough to directly refer to the help and examples of msdn. But if you want to really use it well and accurately, or want to develop some WEB components based on the callback mechanism, then you must first have an in-depth understanding of the callback implementation mechanism. In this article, Teddy will work with you to analyze the entire invocation and feedback mechanism of callback. I believe it will be of certain benefit to help you use callback better.
Callback vs Atlas
First, let’s talk about Atlas. Many friends may find it strange that there is already Callback, why do we need to release Atlas again? Regarding this issue, I have not investigated how the author of Atlas explains it. But from my personal experience of using callback and atlas, I feel that callback as an interface is very similar to postback, and it must be to allow users to use it similarly to postback. However, its postback-like mechanism should be said to be not particularly convenient to use and not easy to expand. Of course, this is compared to other AJAX framework implementations. Therefore, Microsoft has learned from many existing AJAX implementations, such as Prototype, Backbase and AJAX.NET, and combined it with some of the unique functions of ASP.NET 2.0 to create such an AJAX framework that draws on the strengths of others. It is difficult to quantify how good it is to develop AJAX applications based on Atlas, but it is definitely not worse than other AJAX frameworks, plus the backend of Microsoft and the applications of heavyweight sites like live.com Promotion, its impact is certainly worth looking forward to.
However, this does not mean that callback implementation is useless. As programmers, we need to have the correct attitude and use the most correct technology in the correct use case. No framework is omnipotent and suitable for any use environment; just like everyone is debating which software development method is best, CMMi, RUP, XP, AGILE~~, in fact, there is no best, the most suitable is the most suitable. OK What we should do most is to understand the principles, advantages and disadvantages of various solutions, so that we can rationally use the right tools to solve practical problems.
Begin from Client Script
We all know that at the bottom level, all AJAX has nothing more than two implementation mechanisms: XMLHTTP and IFRAME. Before the word AJAX gained widespread attention, in fact, functional frameworks based on these two underlying implementations, or non-refresh effect implementations based on these two technologies, were already widely used. Of course, with today's development, in terms of using interfaces, the details of these underlying mechanisms are often hidden by the framework, and using interfaces has become increasingly simple. Users only need to call these simple interfaces, and there is no need to know how to achieve the specific effect.
However, since we are here to analyze the callback implementation mechanism, let us start with a callback call client script call to see how Microsoft implements this callback mechanism.
1. ClientScript.GetCallbackEventReference(...)
To trigger a callback, first of course, a call needs to be issued in the client script. A typical calling 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(...) will return the actual callback script according to the passed parameters. This function has multiple overloaded versions, so you can refer to MSDN for the meaning of these parameters. Take the specific parameters in the above sample code:
- this means that the server control that executes the callback is the current Page. The current Page must implement the ICallbackEventHandler interface, including the string GetCallbackResult() and void RaiseCallbackEvent(eventArgument). For 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 RaiseCallbackEvent, which allows people to A string that defines the format;
- "ReceiveServerData" 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 this function can contain two parameters, for example:
<script type="text/javascript">
functionReceiveServerData(result, context)
{}
</script>
These two parameters are the return data result of the callback, and the context parameter that is returned unchanged when we trigger the callback. Of course, these two parameters are of string type.
- There is no need to explain "context". Just remember that this parameter will be passed intact to the specified return data processing function. MSDN's official documentation says that context can generally be used to pass script code that needs to be called in the client's return data processing function, but in fact, you can pass anything. Think of it as a trigger callback from the client. , just go to the parameter transfer channel between the receiving segments that process the returned data.