Use the ICallbackEventHandler interface to implement Ajax more efficiently

Source: Internet
Author: User

The ICallbackEventHandler interface can be used to conveniently and efficiently implement Ajax functions. 1. The ICallbackEventHandler interface must be implemented for page processing. This interface has two methods: a and GetCallbackResult. The method returns the processing result to the client, after the request is complete, B and RaiseCallbackEvent are automatically called. This method is used to obtain data from the client and received by the eventArgument parameter, result 2 is obtained by performing relevant processing. register the callback and the master script a, and register the callback function string reference = Page. clientScript. getCallbackEventReference (this, "arg", "SendData", "context"); registers a reference to the callback function to obtain the returned results and perform business logic processing, the registered function name and the function name written by the client must be the same in order to reference B. register the main function, the main function, such as the string re request sent by the server. GCallbakeScript = "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 directly to send the request [csharp] server code [csharp] /// <summary> /// implement the ICallbackEventHandler interface, faster Ajax functions // </summary> public partial class _ Default: System. web. UI. page, IC AllbackEventHandler {private string returnValue = string. empty; protected void Page_Load (object sender, EventArgs e) {// register the callback script and write it by yourself. This script receives the returned result string reference = Page. clientScript. getCallbackEventReference (this, "arg", "SendData", "context"); // register the processing script string regCallbakeScript = "function CallServer (arg, context) {"+ reference +"} "; Page. clientScript. registerClientScriptBlock (this. getType ()," CallServer ", regCallbakeScript, true );} # region ICallbackEventHandler member // <summary> // return result /// </summary> /// <returns> </returns> public string GetCallbackResult () {if (string. isNullOrEmpty (returnValue) return (returnValue = "-1"); return returnValue;} // <summary> // triggers the RaiseCallbackEvent event to obtain client data, then process it. // </summary> /// <param name = "eventArgument"> </param> public void raisecallbackpa Ent (string eventArgument) {returnValue = eventArgument;} # endregion} [javascript] client script code: [javascript] <script type = "text/javascript"> // receives the callback result, this function is automatically called back after processing the server. It is equivalent to the callback function SendData (arg) {alert (arg);} in Ajax );} // client logic function GetReturnValue () {var arg = "Hello CallbackEventHandler! "; // Call the script registered after the page is loaded. Pass the parameter 'arg 'to the server for processing. The second parameter 'context' does not need to pass CallServer (arg ,"");} </script> <input type = "button" value = "CallbackMe" id = "btn" onclick = "GetReturnValue ()"/>

Related Article

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.