Use the ICallbackEventHandler interface to implement lightweight Ajax effects.

Source: Internet
Author: User

Use the ICallbackEventHandler interface to implement lightweight Ajax effects.

1. There are many implementations of ICallbackEventHandler on the Internet. There are several reasons for continuing to write about this interface: (1) After a long time, it is easy to forget how to use this interface, you must search again. (2) Search for a bunch of other people's blogs, and watch others write uncomfortably, so you can write what you want. (3) When I saw my own blog, I quickly realized how to use it. (4) Another reason is that the project is not tight recently and you do not know what to learn. Just a few lines of words can be typed.

2. The ICallbackEventHandler interface makes up for the shortcomings of the Ajax framework! The Ajax framework is relatively large and will generate a lot of Js Code on the front-end. The ICallbackEventHandler interface is not that troublesome. Its logic, that is, the front-end control trigger (click, doubleclick, mouseover, blur, focus), no matter which trigger method, let him specify a Js method for executing the foreground (this method is registered to the foreground using RegisterClientScriptBlock () method in the background Page_load ). Then, this Js method will execute a method in the background. The background will assign the processing result to an Attribute Based on the parameter and then return the value of this attribute in the background callback method, the method that has already been registered on the front-end of the page will be processed based on the returned value.

3. Based on this logic, I wrote a small Demo to determine whether the current mobile phone number has been registered based on the mobile phone number entered by the user.

(1) Front-end HTML code

<Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> </title> <script src =" Scripts/jquery-1.4.1.min.js "type =" text/javascript "> </script> <script type = "text/javascript"> function exploreserverdatanew (returnVal) {$ ("# warn" ).html (returnVal) ;}$ (function () {.( "input [id * = 'tb _ lelephone ']"). blur (function () {var mobilePhone = $ ("input [id * = 'tb _ lelephone ']"). val (); callServerEvent (mobilePhone ,"");});}) </script> 

 

(2) Background code

Public partial class _ Default: System. web. UI. page, ICallbackEventHandler {protected void Page_Load (object sender, EventArgs e) {// gets a reference to a client function. When this function is called, calls back a client that starts a server-side event. The client function of this overload method contains the specified controls, parameters, client scripts, and context String cbReference = Page. clientScript. getCallbackEventReference (this, "arg", "receiveServerDataNew", "context"); String callbackScript = "function callServerEvent (arg, context)" + "{" + cbReference + ";} "; // register the script Page to the foreground. clientScript. registerClientScriptBlock (this. getType (), "callServer", callbackScript, true);} private string _ CallBackResult = ""; public string GetCallbackResult () {return _ CallBackResult;} public void RaiseCallbackEvent (string eventArgument) {if (eventArgument = "") {_ CallBackResult = "the mobile phone number cannot be blank";} // check the database by yourself. if (eventArgument = "18866668888 ") {_ CallBackResult = "the current mobile phone number has been registered ";}}}

4. When the page is loaded, first execute the Page_Load event in the background, and then the callServerEvent method is registered at the front-end. The front-end page is generated in a lifetime and is registered in the text box of the mobile phone number, causing the focus event to be lost, the callServerEvent () method is executed in an event with no focus (two parameters are passed in), and the background RaiseCallbackEvent (string eventArgument) method is called. The method is processed by business logic and assigned to the _ CallBackResult attribute, the GetCallbackResult () method returns the processing result and calls the foreground method eseserverdatanew (). This method performs page processing based on the background processing result.

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.