How to Implement callback in ASP. NET, ASP. NET implements callback
I. Introduction
In ASp. in the default model of the. NET web page, the user submits the page by clicking a button or other operations. At this time, the client submits all the data in the current page form (including some automatically generated hidden fields) are submitted to the server, the server will re-instantiate an instance of the current page class to respond to this request, and then re-Send the content of the entire page to the client. This processing method has no impact on the running results, but page sending back will cause processing overhead,Thus reducing performanceAnd the user has to wait for processing and re-create the page. Sometimes, we only need to pass part of the data without submitting the entire form, this default processing method (referring to the method of submitting the entire form for sending back) is a little tricky. There are three solutions: Pure JS implementation, Ajax technology, and callback technology, here we will only introduce the implementation of Asp.net callback technology. (The essence of callback is Ajax call. The reason is that we use classes in Asp.net to implement callback. Classes in Asp.net will help us with Ajax operations ).
II. Implementation steps
The main points of implementing the brushless newest page using callback technology are:
1. Implement the current pageICallbackEventHandlerInterface, which defines two methods: The GetCallbackResult method and the RaiseCallbackEvent method. The GetCallbackResult method returns the result of the callback method with the control as the target; the RaiseCallbackEvent method is a callback method for processing controls.
2. provide two JS scripts for the current page. One is the client method to be executed after the client successfully calls the server method, one is the client method to be executed after the client fails to call the server method.
The Code on the test page is:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Register. aspx. cs" Inherits = "ASPNETClientCallBackWithoutPostBack. Register" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> User Registration </title> <script language =" javascript "> // call the client method function Success called when the server is successful (arg, context) {document. getElementById ("message "). innerHTML = arg;} // call the client method function Error (arg, context) {document. getElementById ("message "). innerHTML = "exception" ;}</script>
The backend CS code is:
Using System; using System. web. UI; namespace ASPNETClientCallBackWithoutPostBack {public partial class Register: System. web. UI. page, ICallbackEventHandler {string result = string. empty; protected void Page_Load (object sender, EventArgs e) {// obtain the ClientScriptManager object of the current Page, which is used to manage the client step ClientScriptManager clientScriptManager = Page. clientScript; // get callback reference // execute the following code to generate the WebForm_DoCallback method on the client and call it to achieve asynchronous call This method is ASP. the method automatically generated by NET will be sent to the client string reference = clientScriptManager. getCallbackEventReference (this, "arg", "Success", "", "Error", true); string callBackScript = "function CallServerMethod (arg, context) {"+ reference +" ;}"; // register the client script with the current page // CallServerMethod is the key of the client script to be registered clientScriptManager. registerClientScriptBlock (this. getType (), "CallServerMethod", callBackScript, true);} // <summary> // server The callback method run by the server // </summary> /// <param name = "eventArgument"> </param> public void RaiseCallbackEvent (string eventArgument) {if (eventArgument. toLower (). indexOf ("admin ")! =-1) {result = eventArgument + "user registered";} else {result = eventArgument + "registrable ";}} /// <summary >/// return the execution result of the callback method /// </summary> public string GetCallbackResult () {return result ;}}}
When we view the above Asp.net Page in a browser, the Asp.net Page will generate standard HTML code after processing the Page class on the server side. The specific code is as follows:
<Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <title> User Registration </title> <script language =" javascript "> // call the client method function Success (arg, context) called when the server is successful) {document. getElementById ("message "). innerHTML = arg;} // call the client method function Error (arg, context) {document. getElementById ("message "). innerHTML = "exception" ;}</script>
Iii. Running results
Next, let's take a look at the effects of the above Code to implement a refreshing callback:
Iv. Summary
Because I have been learning the content of Asp.net For A While recently, I will record some important content during the learning process and hope it will be helpful to other friends.
Articles you may be interested in:
- Implement progress bar using Asp. Net callback
- Asp.net Page. EnableEventValidation attribute to verify the Server Control's sending and callback events
- C # Three call examples of delegation (synchronous call asynchronous callback)
- C # example of communication using callback events between program forms
- C # solution to the MessageBox carriage return (Enter) Key callback problem in the KeyUp event
- Asp.net Callback technology Callback Study Notes
- C # callback mechanism analysis
- Example of asynchronous callback function usage in C #