Clientcallback provided by ASP. net2.0

Source: Internet
Author: User

Now Ajax is very popular. Do not forget the clientcallback provided by ASP. net2.0, which is also very useful!
Now let's talk about the simple implementation of clientcallback:
1. First, add the icallbackeventhandler interface in the Aspx. CS file page class.

Public partial class _ default: system. Web. UI. Page, icallbackeventhandler

Icallbackeventhandler interface is very important in clientcallback. Let's take a look at what the icallbackeventhandler interface has:
Description
String getcallbackresult () returns the result of a callback event with the control as the target.
Void raisecallbackevent (string eventargument) processes control-oriented callback events. Eventargument parameter: indicates to be passed to event processingProgramEvent parameters.

2. To implement the script callback, you must write the script:

// Trigger event JS
String cbref = page. clientscript. getcallbackeventreference (this, "Arg", "jscallback", "context ");
String cbscr = string. Format ("function usecallback (ARG, context) {{{ 0 }}}", cbref );
Page. clientscript. registerclientscriptblock (this. GetType (), "usecallback", cbscr, true );
This sectionCodeIt is set to trigger the script callback event function, callback function, and exception handling function through the Code registerclientscriptblock. Usecallback (ARG, context) function: triggers server-side event functions; Arg is the event parameter to be passed to the event handler, and context is the context parameter (rarely used, you can get this value in the following jscallback callback function, but I cannot get this value on the server)

// Callback JS
String context1 = "<SCRIPT type = \" text/JavaScript \ "> function jscallback (ARG, context) {document. forms [0]. textbox1.value = ARG; window. alert (context) ;}</SCRIPT> ";
Page. clientscript. registerclientscriptblock (this. GetType (), "jscallback", context1 );
This Code uses registerclientscriptblock to register the callback js code. You can also directly write it to the webpage. Use of callback JS:

Jscallback (ARG, context) function: In JS, The jscallback function is used to process parameters returned by the server (for example, the XML parameters returned are displayed in the drop-down box after processing ).

Clientcallback running process:
When the page executes usecallback (ARG, context), it automatically runs raisecallbackevent (string eventargument) on the server, then getcallbackresult (), and finally executes the jscallback (ARG, context) function on the page.

3. Select a trigger event condition:
Is to let the page execute the function that triggers the server event. Example: button2.attributes. Add ("onclick", "usecallback ('gyf', 'gyf2 ');");

Haha, there is no refresh in this way. It's easy! I have a limited level of writing skills.
See the code for details: Download the code

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.