Refresh | no refresh
The no refresh callback advocated by Ajax technology requires a large amount of JavaScript code to be written in the original technology or a few Ajax frameworks to make development efficiency and maintainability much less. In fact, asp.net2.0 has provided such an interface, this is ICallbackEventHandler.
There have been many articles on the ICallbackEventHandler Web site, this one is to gild the lily. ICallbackEventHandler exist in the System.Web.UI, let's try a very simple example first.
The first step is to create a new web window in the VS2005.
In the second step, in ASPX, put a section of HTML code (as follows):
1<body>
2 <form id= "Form1" runat= "Server" >
3 <div>
4 <button >CallServer</button>
5 </div>
6 </form>
7</body>
Step three, then put a JavaScript script in <HEAD></HEAD>:
1 <script type= "Text/javascript" >
2 function CallServer ()
3 {
4 var product = "Test";
5 <%= clientscript.getcallbackeventreference (This, "product", "Receiveserverdata", null)%>;
6}
7
8 function Receiveserverdata (rValue)
9 {
alert (RValue);
11}
</script>
The fourth step, in this ASPX background CS code, inherits the ICallbackEventHandler interface and implements two methods in the interface:
Icallbackeventhandler.getcallbackresult () and Icallbackeventhandler.raisecallbackevent (string eventArgument)
The fifth step, adding a variable callbackvalue, and modifying the interface two methods are:
1 private String callbackvalue = String. Empty;
2
3 String Icallbackeventhandler.getcallbackresult ()
4 {
5 return Callbackvalue + ", OK";
6}
7
8 void Icallbackeventhandler.raisecallbackevent (String eventargument)
9 {
this. Callbackvalue = eventargument;
11}
12
The sixth step, the operation, the interface will appear a button, click, will "test" this string to the background, the background C # code will be added to the string ", OK" after return to the client's JavaScript code, and display.
The above six steps can be implemented without a flush callback. Now, let's analyze a few pieces of code.
First look at the JavaScript code in step three, in which the CallServer () method has a callback, and the callback statement is:
<%= Clientscript.getcallbackeventreference (This, "product", "Receiveserverdata", null)%>;
The second parameter in the four parameter specifies that the string variable in this JavaScript in product is passed back to the background, and the third parameter specifies the JavaScript method Receiveserverdata (string Value) that receives the return information when returned from the background.
The two methods in the background in step fifth, a icallbackeventhandler.raisecallbackevent (string eventargument), are used to receive string variables from the foreground JavaScript. and assign the value to the internal variable this. Callbackvalue, another method Icallbackeventhandler.getcallbackresult () will change the internal variable this. Callbackvalue is returned to the foreground JavaScript method Receiveserverdata (string Value).
The order of the calls is: (foreground) CallServer ()--> (background) icallbackeventhandler.raisecallbackevent (String eventargument)--> (background) Icallbackeventhandler.getcallbackresult ()--> (foreground) receiveserverdata (string Value).
The whole call process is very simple, and one of the most critical steps is the third step
<%= Clientscript.getcallbackeventreference (This, "product", "Receiveserverdata", null)%>;
This method, the following is from the Internet to find a piece of information, we can see.
GetCallbackEventReference enables the client method to be reclaimed at the end of a client request. It also lets callbackmanager determine what kind of callback method is produced. The overloaded methods used in this example are:
public string getcallbackeventreference (
string target, string Argument,
string clientcallback, string context,
String clientErrorCallback)
Table 1. The parameter description of the GetCallbackEventReference method.
Parameters Description Target ID of the page where the callback invocation is handled. For more, overloaded options available in the next immediate section. In our sample ' this ' is the argument value, since the callback are handled in the same page. the ' is ' is the par Ameter defintion used to send value to the server. This value was received by parameter ' eventargument ' at the ' server end ' using the ' raisecallbackevent event. ' Arg "becomes the" the "parameter name in our sample. The value is passed through this argument from the client. Clientcallback method Name of the callback this is invoked after successful server call. CallbackHandler ' is the ' method ' name that handles the Callback. Context A parameter which associated with the "argument" from the client. It usually should is used to identify the "context of" the call. You'll understand this better from the sample implementation. In the sample "CTX" is just another parameter definition used. The value is passed from the client. clientErrorCallback Name of the method this is called from the Callbackmanager in case of any errors.
The string returned from this method is:
__docallback (' __page ', Arg,callbackhandler,ctx, Errorcallback)
Another overloaded method is:
public string GetCallbackEventReference (
Control control, string argument,
String clientcallback, String context)
public string GetCallbackEventReference (
Control control, string argument,
String Clientcallback, String context,
String clienterrorcallback)