Using the ICallbackEventHandler interface for more efficient Ajax

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.