The refresh-free callback advocated by AJAX technology requires writing a large amount of JavaScript code or using some AJAX frameworks in the original technology, which greatly reduces development efficiency and maintainability. In fact, ASP.NET2.0 already provides such an interface, which is ICallbackEventHandler.
There are already many articles on ICallbackEventHandler on the Internet, and this article really adds more to the story.
ICallbackEventHandler exists in System.Web.UI. Let's try it out with a very simple example.
The first step is to create a new WEB window in VS2005.
The second step is to put a piece of HTML code (below) in ASPX:
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <button onclick="CallServer()">CallServer</button>
5 </div>
6 </form>
7</body>
The third step is to 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 functionReceiveServerData(rValue)
9 {
10 alert(rValue);
11 }
12 </script>
The fourth step is to inherit the ICallbackEventHandler interface in the background CS code of ASPX and implement the two methods in the interface:
ICallbackEventHandler.GetCallbackResult()
and
ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
The fifth step is to add a variable CallBackValue and modify the two methods of the interface as:
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 {
10 this.CallBackValue = eventArgument;
11 }
12
The sixth step is to run. A button will appear on the interface. After clicking, the string "Test" will be transmitted to the background. The background C# code will add ", OK" to the string and return it to the client's JavaScript code and display it. .
By following the above six steps, you can achieve refresh-free callback. Now, let's analyze a few pieces of code.
Let’s first look at the JavaScript code in the third step. There is a callback in the CallServer() method. The callback statement is:
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
The second parameter among the four parameters specifies that the string variable in the product JavaScript is returned to the background, and the third parameter specifies the JavaScript method ReceiveServerData(string Value) that receives the return information when returning from the background.
In the fifth step, there are two methods in the background. One, ICallbackEventHandler.RaiseCallbackEvent(string eventArgument), is used to receive the string variable passed in the foreground JavaScript and assign it to the internal variable this.CallBackValue. The other method, ICallbackEventHandler.GetCallbackResult(), will change the The internal variable this.CallBackValue is returned to the foreground JavaScript method ReceiveServerData(string Value).
The calling sequence is: (frontend)CallServer() --> (backend)ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) --> (backend)ICallbackEventHandler.GetCallbackResult() --> (frontend)ReceiveServerData(string Value).
The whole calling process is very simple, and the most critical step is the third step
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
For this method, the following is a piece of information found on the Internet, you can take a look.
GetCallbackEventReference causes the client method to be recycled at the end of the client request. It also lets the CallBackManager determine which callback method to generate. The overloaded method used in this example is:
public string GetCallbackEventReference(
string target, string argument,
string clientCallback, string context,
string clientErrorCallback)
Table 1. Parameter descriptions for the GetCallBackEventReference method.
Parameters Description target ID of the page where the callback invocation is handled. For more see the other overloaded options available in the next immediate section.In our sample "this" is the argument value, since the callback is handled in the same page. This is the parameter defintion used to send value to the server. This value is received by parameter "eventArgument" at the server end using the RaiseCallbackEvent event."arg" becomes the first parameter name in our sample. The value is passed through this argument from the client. clientCallback Method name of the callback that is invoked after successful server call. "CallBackHandler" is the method name that handles the callback. context A parameter that is associated with the "argument" from the client. It usually should be used to identify the context of the call. You will understand this better from the sample implementation.In the sample "ctx" is just another parameter definition used. The value for this is passed from the client. clientErrorCallback Name of the method that 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)