Step 6: Use icallbackeventhandler to implement the callback without refreshing

Source: Internet
Author: User

Step 6: Use icallbackeventhandler to implement the callback without refreshing

The non-refreshing callback promoted by Ajax technology requires a lot of JavaScript code or some Ajax frameworks in the original technology, which greatly reduces the development efficiency and maintainability. In fact, this interface has been provided in ASP. net2.0, Which is icallbackeventhandler.
There are many articles on icallbackeventhandler. This article is a perfect addition.

Icallbackeventhandler exists in system. Web. UI. Let's use a very simple example.

Step 1: Create a new Web window in vs2005.
Step 2: In aspx, put a piece of HTML code (as follows ):

1 <body>
2 <Form ID = "form1" runat = "server">
3 <div>
4 <button onclick = "callserver ()"> callserver </button>
5 </div>
6 </form>
7 </body>

Step 3: Put a Javascript script in 1 <SCRIPT type = "text/JavaScript">
2 function callserver ()
3 {
4 var Product = "test ";
5 <% = clientscript. getcallbackeventreference (this, "product", "eseserverdata", null) %>;
6}
7
8 function exploreserverdata (Rvalue)
9 {
10 alert (Rvalue );
11}
12 </SCRIPT>

 

Step 4: Inherit the icallbackeventhandler interface in the backend CS code of aspx and implement the two methods in the interface:
Icallbackeventhandler. getcallbackresult ()
And
Icallbackeventhandler. raisecallbackevent (string eventargument)

Step 5: add the callbackvalue variable and modify the two methods of the interface as follows:

1 private string callbackvalue = string. empty;
2
3 string icallbackeventhandler. getcallbackresult ()
4 {
5 return callbackvalue + ", OK ";
6}
7
8 void icallbackeventhandler. raisecallbackevent (string eventargument)
9 {
10 This. callbackvalue = eventargument;
11}
12

 

Step 6: Run. A button is displayed on the interface. After you click it, the "test" string is passed to the background. The C # code in the background adds the string ", "OK", return the JavaScript code to the client and display it.

In the above six steps, you can implement a callback without refreshing. Now let's analyze several pieces of code.
First look at the JavaScript code in step 3, where the callserver () method performs a callback, the callback statement is:
<% = Clientscript. getcallbackeventreference (this, "product", "eseserverdata", null) %>;

The second parameter in the four parameters specifies that the string variable in the Javascript product is returned to the background, and the third parameter specifies the JavaScript method javaseserverdata (string value) that receives the returned information when returned from the background ).

Step 5: Two backend methods, one icallbackeventhandler. raisecallbackevent (string eventargument) is used to receive string variables from Javascript in the foreground and assign them to the internal variable this. callbackvalue, another method is icallbackeventhandler. getcallbackresult () will change the internal variable this. callbackvalue is returned to the front-end JavaScript method javaseserverdata (string value ).

The call sequence is as follows: (foreground) callserver () --> (background) icallbackeventhandler. raisecallbackevent (string eventargument) --> (background) icallbackeventhandler. getcallbackresult () --> (foreground) receiveserverdata (string value ).

The entire call process is very simple, and the key step is step 3.
<% = Clientscript. getcallbackeventreference (this, "product", "eseserverdata", null) %>;
Here is a piece of information from the Internet.

Getcallbackeventreference recycles client methods at the end of client requests. It also allows callbackmanager to determine which callback method is generated. The method used in this example is:

Public String getcallbackeventreference (
String target, string argument,
String clientcallback, string context,
String clienterrorcallback)
Table 1. parameter description of the getcallbackeventreference method.
Parameters description target ID of the page where the callback invocation is handled. for more see the other overloaded options available in the next immediate section. in our sample "this" is the argument value, since the callback is handled in the same page. argument this is the parameter defintion used to send value to the server. this value is already ed by parameter "eventargument" at the server end using the raisecallbackevent event. "Arg" becomes the first parameter name in our sample. the value is passed through this argument from the client. clientcallback Method Name of the callback that is invoked after successful server call. "callbackhandler" is the method name that handles the callback. context a parameter that is associated with the "argument" from the client. it usually shocould be used to identify the context of the call. you will understand this better from the sample implementation. in the sample "CTX" is just another parameter definition used. the value for this is passed from the client. clienterrorcallback name of the method that is called from the callbackmanager in case of any errors.
The string returned from this method is:


_ Docallback ('_ page', ARG, callbackhandler, CTX, errorcallback)
 
Another overload method is:

Public String getcallbackeventreference (
Control, string argument,
String clientcallback, string context)

Public String getcallbackeventreference (
Control, string argument,
String clientcallback, string context,
String clienterrorcallback)

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.