Asp. How to implement callback _ practical skills in net

Source: Internet
Author: User
Tags cdata

First, the introduction
In the default model of the 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 callback technology. (The nature of the callback is actually the AJAX call, and that's because we use the classes in to implement the callback, and the class in 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://

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 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 page in the browser, the 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 content, here to record some of the learning process of the individual feel more important content, I hope to some other friends to help.

Related Article

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: 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.