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 during client callback.
Public void RaiseCallbackEvent (String eventArgument)
// This method will be 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. IsReusable
Void 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 the assembly information.
// Czhenq. AJAX. Class1.Dencode is a custom string encoding method.
String AssemblyName = Czhenq. AJAX. Class1.Dencode (context. Request. QueryString ["as"]);
// Obtain method parameters
String Arguments = context. Request. QueryString ["ar"]; // start to call the 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 parameters
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.
// Only convert the parameter to Int32.
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 ");
}
// Processing ends
Context. Response. End ();
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page