ASP. NET 2.0 callback enables the ASP. NET Server Side Control to call the server side method without executing a complete PostBack process. That is to say, the server side control can support Ajax well.
Unlike PostBack, callback only sends viewstate and user-defined information to the server when sending a request to the server. After the request is complete, only user-defined results are returned instead of the render page.
The gridview control uses this technology to implement the refreshing paging and sorting functions. How does it implement it? How does we add this function to the custom control? This article will briefly explain how to use ASP. NET 2.0 callback to implement Ajax.
1. icallbackeventhandler Interface
ASP. NET Server controls can accept client callback events by implementing the icallbackeventhandler interface.
Icallbackeventhandler interface declaration:
1 public interface icallbackeventhandler
2 {
3 string getcallbackresult ();
4 void raisecallbackevent (string eventargument );
5}
Raisecallbackevent is responsible for processing client callback events. The method parameter eventargument is provided by the client script when the callback is triggered. This method will depend on the eventargument parameter for processing.
Getcallbackresult is responsible for returning the processing result to the client script as a string. After callback is complete, the client script updates the Page Based on the obtained processing results.
2. callbackeventreference
So how can I register a client script to trigger callback? What else do I need?
The clientscriptmanager class is used to manage client scripts on Web pages. It provides a series of methods to register scripts and can be referenced by specified client script functions. Using the getcallbackeventreference method of the clientscriptmanager class, we can obtain a reference to the client function. When the function is called on the client, a client callback is started.
Getcallbackeventreference method declaration:
Public String getcallbackeventreference (
Control,
String argument,
String clientcallback,
String context,
String clienterrorcallback,
Bool useasync
)
The first parameter refers to the server-side control that implements the icallbackeventhandler interface;
The second parameter will be passed to the raisecallbackevent method executed on the server. It can be a JavaScript function call expression;
The third parameter is a JavaScript function name. After callback is complete, the function will be called, and the execution result of the server-side function getcallbackresult will also be used as the parameter of this function;
The fourth parameter is the context of the currently executed callback. This parameter can also be a JavaScript function call expression;
The fifth parameter is a JavaScript function name. If an error occurs during callback execution, the function will be called.
The sixth parameter is a bool value to determine whether the current callback should be executed synchronously or asynchronously.
After obtaining the reference of this callback client function, we can register a new client function to call it. Then the client can call back through the newly registered function.
3. Example
We use a simple example to analyze the entire execution process of ASP. NET 2.0 callback:
1 public class mycontrol: webcontrol, icallbackeventhandler
2 {
3 private const string script1 = "function oncallbackcomplete (result) {/N" +
4 "Var element = Document. getelementbyid (?? % ID % ??); /N "+
5 "If (element! = NULL)/n "+
6 "element. innerhtml = result;}/N ";
7
8 private const string script2 = "function oncallbackerror () {/N" +
9 "Var element = Document. getelementbyid (?? % ID % ??); /N "+
10 "If (element! = NULL)/n "+
11 "element. innerhtml = ?? Error ??;} /N ";
12
13 Public String getcallbackresult ()
14 {
15 return "Callback result ";
16}
17
18 public void raisecallbackevent (string eventargument)
19 {
20}
21
22 public override void renderbegintag (htmltextwriter writer)
23 {
24 writer. addattriback (htmltextwriterattribute. onclick, "doclientcallback ()");
25 base. renderbegintag (writer );
26 writer. Write ("My callback control ");
27}
28
29 protected override void onprerender (eventargs E)
30 {
31 // define callback references.
32 string callbackref = This. Page. clientscript. getcallbackeventreference (
33 this, "", "oncallbackcomplete", null, "oncallbackerror", true );
34
35 // register script blocks will perform call to the server.
36 This. Page. clientscript. registerclientscriptblock (
37 This. GetType (), "doclientcallback ",
38 "function doclientcallback () {" + callbackref + "}/N", true
39 );
40
41 // register other scripts
42 This. Page. clientscript. registerclientscriptblock (
43 This. GetType (), "oncallbackcomplete ",
44 script1.replace ("% ID %", this. clientid), true );
45 this. Page. clientscript. registerclientscriptblock (
46 this. GetType (), "oncallbackerror ",
47 script2.replace ("% ID %", this. clientid), true );
48
49 base. onprerender (E );
50}
Place the preceding control on a page. When you click the control at runtime, a callback is executed and the control content is updated.
Execution sequence:
Specifically, webform_docallback and webform_callbackcomplete are methods in the Microsoft JavaScript library.
ASP. NET 2.0 callback provides a simple method to enable the ASP. NET Server segment control to support Ajax. It can be considered as a lightweight PostBack.
The full text is complete.
This article is transferred from
Http://www.cnblogs.com/tedzhao/archive/2008/05/26/1206514.html