[ASP. NET 2.0] Use icallbackeventhandler in Step 6 to implement brushless newest callback
========================================================== ================================
Step 6: Use icallbackeventhandler to implement the callback without refreshing
The non-refreshing callback promoted by Ajax technology requires a lot of JavaScriptCodeOr use some Ajax frameworks to greatly reduce development efficiency and maintainability. In fact, this interface has been provided in ASP. net2.0, Which is icallbackeventhandler.
There are already many icallbackeventhandler onlineArticleIntroduced, this article is actually superfluous.
Icallbackeventhandler exists in system. Web. UI. Let's use a very simple example.
Step 1Create a new Web window in vs2005.
Step 2In aspx, put a piece of HTML code (as follows ):
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Button onclick = "callserver ()"> callserver </button>
</Div>
</Form>
</Body>
Step 3And then put a Javascript script in
<SCRIPT type = "text/JavaScript">
Function callserver ()
{
VaR Product = "test ";
<% = Clientscript. getcallbackeventreference (this, "product", "eseserverdata", null) %>;
}
Function compute eserverdata (Rvalue)
{
Alert (Rvalue );
}
</SCRIPT>
Step 4In the backend CS code of aspx, inherit the icallbackeventhandler interface and implement two methods in the interface:
Icallbackeventhandler. getcallbackresult ()
And
Icallbackeventhandler. raisecallbackevent (string eventargument)
Step 5Add the callbackvalue variable and modify the interface using the following two methods:
Private string callbackvalue = string. empty;
String icallbackeventhandler. getcallbackresult ()
{
Return callbackvalue ", OK ";
}
Void icallbackeventhandler. raisecallbackevent (string eventargument)
{
This. callbackvalue = eventargument;
}
Step 6, Run. A button will appear on the interface. After you click it, the "test" string will be passed to the background, and the C # code in the background will add ", "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) %>;
This method,
========================================================== ======================================
The following 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)