Ajax features are easily and efficiently implemented using the ICallbackEventHandler interface
1, the processing page needs to implement the ICallbackEventHandler interface, this interface has two methods
A, GetCallbackResult This method returns processing results to the client, which is automatically called after the request is completed
B, RaiseCallbackEvent This method is to obtain data from the client is received by the parameter eventargument, and the relevant processing results are obtained
2. Registration callback and Keynote script
A, register callback function String reference = Page.ClientScript.GetCallbackEventReference (this, "Arg", "SendData", "context");
Register a reference to the callback function to get the return result, and business logic processing, the registered function name and the client write function name must be the same to reference
b, register the master function, the key function, like the server sends the request string regcallbakescript = "function CallServer (arg,context) {" + Reference + "}";
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "CallServer", Regcallbakescript, True);
C, client script: A callback function Fucntion SendData (arg,context) {alert (arg);}
Call the CallServer function to send the request directly
/// <summary> ///implement ICallbackEventHandler interface, complete ajax faster function/// </summary> Public Partial class_default:system.web.ui.page,icallbackeventhandler {Private stringReturnValue =string. Empty; protected voidPage_Load (Objectsender, EventArgs e) { //register the callback script, specifically implement your own write, this script receives the returned results stringReference = Page.ClientScript.GetCallbackEventReference ( This,"Arg","SendData","Context"); //Registering processing Scripts stringRegcallbakescript ="function CallServer (arg,context) {"+ Reference +"}"; Page.ClientScript.RegisterClientScriptBlock ( This. GetType (),"CallServer", Regcallbakescript,true); } #regionICallbackEventHandler Members/// <summary> ///return Results/// </summary> /// <returns></returns> Public stringGetCallbackResult () {if(string. IsNullOrEmpty (returnvalue))return(returnvalue ="-1"); returnreturnvalue; } /// <summary> ///trigger the RaiseCallbackEvent event to get the client data and then process it/// </summary> /// <param name= "eventargument" ></param> Public voidRaiseCallbackEvent (stringeventargument) {returnvalue=eventargument; } #endregion }
Client Script code:
<script type= "Text/javascript" > // receive callback results, this function automatically callback after processing server completes, that is, callback function, rather the callback function in Ajax function SendData (ARG) { alert (arg); } // client-side logic functions function Getreturnvalue () { var arg = "Hello callbackeventhandler!" ; // call the script to register after the page is loaded, pass the parameter ' arg ' to be processed by the server, the second parameter ' context ' does not pass CallServer (ARG, ""); } </script><input type= "button" value= "Callbackme" id= "btn" onclick= "Getreturnvalue ()"/>
Using the ICallbackEventHandler interface for more efficient Ajax