In the process of ASP. NET development, we will encounter this kind of situation: submitting the entire page to obtain the server status causes a great waste. We know that the popular Ajax framework can update partial pages to avoid refreshing the entire page. In fact, ASP. NET also provides similar solutions.
In the default model of the ASP. NET web page, the user interacts with the page, click the button or execute some other operations that cause sending back. At this time, the page and its controls will be re-created, and the page will be run on the serverCodeAnd the new version of the page is displayed in the browser. However, in some cases, you need to run the server code from the client without sending back the code. If the client script on the page maintains some status information (for example, the local variable value), the new copy of the send page and get page will corrupt the status. In addition, page re-sending will cause processing overhead, which will reduce the performance and cause users to have to wait for processing and recreate the page.
To avoid loss of client status and avoid overhead of server round-trip processing, you can encode ASP. NET webpage so that it can execute client callback. In the client callback, the client script function sends a request to the ASP. NET webpage. The webpage runs its normal life cycle version modification-the initialization page, creates its controls and other members, and then calls the method marked specially. This method executes the process written in the code, and then returns the value that can be read by another client script function to the browser. During this process, the page stays in the browser.
The following is a simple example:
Click the button control to display the server time in the text box. As shown in:
The related page code is as follows:
1 < Div >
2 < Input Type = "Text" ID = "Txttime" Runat = "Server" Readonly = "Readonly" />
3 < Input Type = "Button" ID = "Btngettime" Value = "Get" Runat = "Server" />
4 </ Div >
Creating an ASP. NET page that implements client callback is similar to creating any ASP. NET page, but there are also the following differences. Page Server code must implement the icallbackeventhandler interface. This interface requires the raisecallbackevent method and the getcallbackresult method.
1. raisecallbackevent method to process this event
2. The getcallbackresult method returns the callback result.
The Code is as follows:
1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 Txttime. Value = Datetime. Now. tostring ();
4
5 String Callbackref = Clientscript. getcallbackeventreference ( This , String . Empty, " Gettime " , String . Empty );
6 Btngettime. attributes [ " Onclick " ] = Callbackref;
7 }
8
9 Public String Getcallbackresult ()
10 {
11 Return Datetime. Now. tostring ();
12 }
13
14 Public Void Raisecallbackevent ( String Eventargument)
15 {
16 //
17 }
In addition, we also need to implement the client callback JavaScript function code to process the response results and implement partial page updates. The Code is as follows:
1 Function Gettime (){
2 Document. getelementbyid ( " Txttime " ). Value = Arguments [ 0 ];
3 }
4