Client callback provided by Asp. Net 2.0
Asp. Net 2.0 has been released. 2.0 has many new features, and client callback is one of them. Client callback allows us to call methods on the server without sending back. It is consistent with the functions provided by AJAX, but it is not as flexible as AJAX. AJAX can customize the called methods, the callback function provided by 2.0 does not work. To use the client callback function, you must implement the System. Web. UI. IcallbackEventHandler interface.
This interface contains two methods
// This method is fixed when the client calls a callback. public void RaiseCallbackEvent (String eventArgument) // this method is called after RaiseCallbackEvent is executed. The return value of this method will be sent back to the client public string GetCallbackResult () |
Example:
.cs:String cbReference = Page.ClientScript.GetCallbackEventReference(this,"arg", "ReceiveServerData", "context");String callbackScript;callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + "} ;";Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);javascript: |
AJAX Introduction
AJAX is not a new technology, but an organic combination of existing technologies, including XmlHttp and Reflect. An AJAX framework basically includes a custom HttpHandler and a piece of JavaScript code.
AJAX Running Mechanism
In the past, when we used XmlHttp to implement a refreshing page, we used XmlHttp to request a hidden page. net) comes with HttpHandler, and in AJAX, we request a hidden page. The difference is that HttpHandler of this page is implemented by ourselves.
Build your own AJAX:
1. First, we need to implement an Http handler (HttpHandler) to respond to client requests:
To implement a custom HttpHandler, you must implement the IHttpHandler interface.
This interface contains an attribute and a method:
Bool IHttpHandler. isReusablevoid IHttpHandler. processRequest (HttpContext context) Example: bool IHttpHandler. isReusable {get {return true;} void IHttpHandler. processRequest (HttpContext context) {context. response. clear (); // obtain the method to be called string methodName = context. request. queryString ["me"]; // obtain assembly information. // Czhenq. AJAX. class1.Dencode is a custom string encoding method string AssemblyName = Czhenq. AJAX. class1.Dencode (context. request. queryString ["as"]); // obtain the method parameter string Arguments = context. request. queryString ["ar"]; // start to call method Type type = Type. getType (AssemblyName); MethodInfo method = type. getMethod (methodName, BindingFlags. nonPublic | BindingFlags. public | BindingFlags. static | BindingFlags. instance); if (method! = Null) {// use "," to separate string [] args = Arguments. split (",". toCharArray (); ParameterInfo [] paras = method. getParameters (); object [] argument = new object [paras. length]; for (int I = 0; I <argument. length; I ++) {if (I <args. length) {// because all parameters passed by XmlHttp are of the String type, they must be converted. // here, only the parameter is converted to Int32, and other considerations are not taken. Argument [I] = Convert. ToInt32 (args [I]) ;}} object value = method. Invoke (Activator. CreateInstance (type, true), argument); if (value! = Null) context. response. write (value. toString (); else context. response. write ("error");} // process end context. response. end (); |
Client Javascript code:
function CallMethod(AssemblyName,MethodName,Argus){var args = "";for(var i=0;iargs += Argus[i] + ",";if(args.length>0) args = args.substr(0,args.length-1);var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');url = "AJAX/AJAX.czhenq?as=" + AssemblyName + "&me=" + MethodName +"&ar="+ args;xmlhttp.open("POST",url,false);xmlhttp.send();alert(xmlhttp.responseText);} |
3. A simple AJAX framework has been implemented. Now write a code segment to test.