First, the introduction
In the default model of the ASP.net Web page, the user submits the page by clicking a button or another action. At this point, the client submits all data in the current page form, including some automatically generated hidden fields, to the server side, the server instantiates an instance of the current page class to respond to the request, and then resend the contents of the entire page to the client. This approach has little effect on the running results, but page postbacks cause processing overhead, which degrades performance , and lets users have to wait to process and re-create the page, and sometimes we just need to pass part of the data without having to submit the entire form. This default approach (refers to the submission of the entire form for postback) is a bit of a fuss, the solution is mainly three kinds: pure JS implementation, AJAX technology and callback technology, here only to introduce the implementation of the ASP.net callback technology. (The nature of the callback is actually the AJAX call, and that's because we use the classes in ASP.net to implement the callback, and the class in asp.net helps us do ajax operations).
Second, the realization step
The main points of using callback technology to implement a no-refresh page are:
1, let the current page implementation ICallbackEventHandler interface, the interface defines two methods: GetCallbackResult method and RaiseCallbackEvent method, which, The effect of the GetCallbackResult method is to return the result of a callback method that targets the control, and the RaiseCallbackEvent method is to handle a callback method that targets the control.
2, for the current page to provide 2 JS script, one is the client to invoke the server-side method to execute after the success of the client method, one is the client calls the server-side method failed to execute after the client method.
The specific test page code 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
">
Background CS Code is:
Using System;
Using System.Web.UI; namespace Aspnetclientcallbackwithoutpostback {Public partial class Register:System.Web.UI.Page, Icallbackeventhandl ER {string result=string.
Empty; protected void Page_Load (object sender, EventArgs e) {//Gets the ClientScriptManager object for the current page, which is used to manage client footsteps ClientS
Criptmanager ClientScriptManager = Page.clientscript; Get callback reference//execute the following code generates the Webform_docallback method on the client, calling him to achieve an asynchronous call, a method that is asp.net automatically generated and sent to the client string reference = Clie
Ntscriptmanager.getcallbackeventreference (This, "Arg", "Success", "", "Error", true);
String callbackscript = "function Callservermethod (arg, context) {" + reference + ";}"; Registering client script//Callservermethod to the current page is the key clientscriptmanager.registerclientscriptblock of the client script to register (this.
GetType (), "Callservermethod", Callbackscript, True); ///<summary>///Server-side callback method///</summary>///<param name= "eventargument" ></par Am> Public void RaiseCallbackEvent (String eventargument) {if Eventargument.tolower ().
IndexOf ("admin")!=-1 {result = eventargument + "user registered";
else {result = eventargument + "can register";
///<summary>///Returns the execution result of the callback method///</summary> public string GetCallbackResult () {
return result;
}
}
}
When we view the above ASP.net page in the browser, the ASP.net page generates standard HTML code with the processing of the server-side page class, which is as follows:
Iii. Results of operation
Here's a look at the effect of the No refresh callback implemented by the code above:
Iv. Summary
Because the recent period of time in the study of ASP.net content, here to record some of the learning process of the individual feel more important content, I hope to some other friends to help.