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 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. Client JavaScript code:
Function callmethod (assemblyname, methodname, Argus)
{
VaR ARGs = "";
For (VAR I = 0; I
ARGs + = 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.
Use your own Ajax
1. Create a new website and apply the httphandler you just wrote. Register Your httphandler in the web. config file of the website, indicating that the requests will be processed using the handler you wrote. All requests ending with "czq" are processed using "czhenq. httphandlerfactory.
Type = "czhenq. httphandlerfactory, czhenq. Ajax"/>
2. Add a Web page, copy the script to the page, and add a method to be called.
Private string add (int I, Int J)
{
Return textbox1.text;
}
3. Place a hiddenfield control on the page and name it assemblyname. Add the following code to page_load:
String assemblyname = czhenq. Ajax. class1.encode (
Typeof (_ default). assemblyqualifiedname );
Assemblyname. value = assemblyname;
4. Add the following script to the page:
VaR assemblyname = Document. getelementbyid ("assemblyname ");
VaR Argus = new array ();
Argus. Push ("100 ");
Argus. Push ("200 ");
Callmethod (assemblyname, "add", Argus)
To sum up, Ajax is not a new technology. It is only an organic combination of some existing technologies. We can simply understand Ajax as an encapsulation of JavaScript calls of XMLHTTP, it changes the code writing method.
Attached encode and dencode implementation:
Public static string encode (string value)
{
Byte [] bytes = asciiencoding. ASCII. getbytes (value );
Return convert. tobase64string (bytes );
}
Public static string dencode (string value)
{
Byte [] bytes = convert. frombase64string (value );
Return asciiencoding. ASCII. getstring (bytes );
}