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