Abandon PostBack, using CallBack (example)

Abstract: test


Source http://blog.yam.com/jerrytung/article/3639749

Since PostBack is through the Submit and Reload, let Client side and Server-side interaction, but controls a long time, a page PostBack to go back and re-Reload come back, will cause the screen to flicker, if insufficient bandwidth, but also lead to abnormal movement is not smooth.

So they want without changing the status page can update web pages Client-side, CallBack mechanism can be used.


To let page support CallBack, you must practice in Page System.Web.UI.ICallbackEventHandler this interface,
and practice two methods of this interface:

String the GetCallbackResult ()
void the RaiseCallbackEvent (String eventArgument)

CallBack entire process principles:
. Step1 Press HtmlButton

step2. CallBack execution mechanism

Step3.CallBack mechanism calls Server side end .aspx.cs Client program background, this step does not cause the PostBack (end time Server 'RaiseCallbackEvent' Client terminal will receive the incoming value [this Example is CallBackButtonArg]. Next, will be back to the Client-through 'GetCallbackResult' through the calculation value)

after completion of treatment Step4.Server end, the chain will Client-side JavaScript function 'ReceiveServerData', so just through 'GetCallbackResult' return value is 'ReceiveServerData' received.

Step5. The results show, complete CallBack mechanism.

Wherein CallBackButtonArg 'ReceiveServerData (arg, context) ' content server to return data (i.e. returned from 'GetCallbackResult' string data), and the second context is a variable that is for client side javascript themselves upon completion of the callback used, i.e. seen above 'JerryTung' this section, this variable is noted that the client side (of course, can be placed from the server side to generate, but does not change when the callback runtime).

Complete program and annotated as follows:

---------------------------------- ------ the Default.aspx --------------------------------

<% @ Page Language = "C #" the AutoEventWireup = "to true" the CodeFile = "the Default .aspx.cs "Inherits =" _ Default " %>



   未命名页面


  


  

      
      

  




===========================================================

----------------------------------Default.aspx.cs----------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
   string strCallback;

    // Client processing value coming from the end, wherein the event trigger parameter is eventArgument
   public void the RaiseCallbackEvent (String eventArgument)
   {
       strCallback = eventArgument + "" + DateTime.Now.ToString ( "HH: mm: SS");
   }

    // back to the Client-side processing to value
   public String the GetCallbackResult ()
   {
       return strCallback;
   }

    void the Page_Load protected (SENDER Object, EventArgs E)
   {
       the JavaScript // CallBack may trigger the establishment of some of the command code
       string cbReference = Page.ClientScript.GetCallbackEventReference (this, " 'CallBackButtonArg'", "ReceiveServerData", " 'JerryTung'");
       // the just-established CallBack JavaScript program, registered on the onclick event HtmlButton of
       CallBackButton.Attributes [ "onclick"] = cbReference ;

        // generates a JavaScript function, and carries out a processing for receiving CallBack mechanisms coming from the Server side return value
       string strScript = "function ReceiveServerData (arg , context) {document.getElementById ( '" + TextBox1.ClientID + "' ) .Value = '#' + Arg + '@' + context;} ";
       // strScript inserted in the page JavaScript programs
       Page.ClientScript.RegisterClientScriptBlock (this.GetType ()," myscript ", strScript, true);
   }
}

================================================ ===========

Original: Large column  abandon PostBack, using CallBack (example)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11458391.html