Local refresh of server-side controls, CallBack

Source: Internet
Author: User

Local refresh of server-side controls, CallBack(2007-01-13 13:46:37) reproduced
Category: Technical data
found. net2.0 originally has a set of CallBack mechanism, can easily implement the server control like Ajax effect (to achieve partial refresh), but really is the server-side control with Ajax, the effect indeed is not ASP .
the intrinsic implementation mechanism of the UpdatePanel control of callback and ASP. Ajax is Ajax
However, pages with UpdatePanel controls still interact with the server in postback mode, updating the ViewState
Callback uses XMLHTTP (get mode) or IFRAME, this process does not involve the ViewState of the page at all ( still not solve the viewstate problem of server-side control, and only support asp.net2.0, there are many defects, not recommended for personal use )

The callback mechanism in http://www.cnblogs.com/teddyma/archive/2005/11/28/286196.html depth analysis asp.net2.0
Http://www.cnblogs.com/jailu/archive/2007/06/26/796045.html Experience asp.net2.0 Client callback function (CallBack)
Http://msdn2.microsoft.com/zh-cn/library/system.web.ui.icallbackeventhandler (vs.80). aspx MSDN

A complete callback consists of the following procedures:
(1) The client makes a callback request;
(2) server-side receive client callback request;GetCallbackEventReference Accept
(3) server-side processing request and postback request to client;raisecallbackevent processing, GetCallbackResult return
(4) The client receives the returned results of the server and updates the HTML.

String Page.ClientScript.GetCallbackEventReference (//Gets a reference to a client function
string target,//name of the server control that handles client callback (implements ICallbackEventHandler interface)
string argument,//1 parameters passed from JS to the server-side RaiseCallbackEvent method
String clientcallback,//the name of the JS function that receives the server's processing results.
String context,//to the specified return data processing JS function intact
String clienterrorcallback,//The JS function that receives the result when an error occurs on the server-side event handler
BOOL UseAsync//true synchronous; false Async
)

void Page.ClientScript.RegisterClientScriptBlock (//Registering client script with the Page object
Type type,//types of client script to register
String key,//name of the client script to register
string script,//client script content to be registered
BOOL addScriptTags//Boolean value indicating whether to add script tags
)


1. To use Callback, you first inherit the ICallbackEventHandler interface:
   public partial class Callback:page, ICallbackEventHandler
    or:
   <%@ Implements interface= " System.Web.UI.ICallbackEventHandler "%>
2. Two methods to implement the ICallbackEventHandler interface
   string getcallbackresult ()//returns a string type (only String type) The data to the client script
    {
     //////
    }
   void raisecallbackevent (String eventargument)//with an incoming parameter of type string that can be used to receive parameters for client script
     {
    ///////
    return "";
    }

3. The page class handling callback pages is also responsible for the management of the client callback script: Indicate which JS function sends the callback request and which JS function receives the data.
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
String strrefrence = Page.ClientScript.GetCallbackEventReference (This, "Arg", "AAA", "context");
Here AAA is callback successful, the client receives the result of the JS method, can contain two parameters.
This indicates that the service-side control executing the callback is the current page, and the current page must implement the ICallbackEventHandler interface
ARG is the value passed to the RaiseCallbackEvent parameter eventargument, which can be used in a custom formatted string.
The context parameter is passed intact to the specified JS function for the returned data processing

String strcallback = "function BBB (arg, context) {" + Strrefrence + "};";

Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "BBB", Strcallback, True); ////Register JS function bbb,bbb trigger GetCallbackEventReference
}
}

It's better to understand.
<script language= "javascript" type= "Text/javascript" >
function BBB (ARG, context)
{<%= clientscript.getcallbackeventreference (this, "Arg", "AAA", "context")%>;}
</script>
● The former writes the JS function bbb on the server side and registers with RegisterClientScriptBlock, and the client calls BBB,BB and calls the GetCallbackEventReference
2nd, the BBB is written directly on the client.


4. Client-side code: Client-side script is relatively simple, only need to implement the JS method to receive callback results (this is AAA) OK
function AAA (valueres, context)//valueres is the result of server-side processing
{
Document.getelementbyidx (elementId1). InnerHTML = Valueres;
Document.getelementbyidx (elementId2). InnerHTML = context;
}

Example: Getting server-side time with callback (12 and 24-hour systems)
Default.aspx.cs
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
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, ICallbackEventHandler
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
String strrefrence = Page.ClientScript.GetCallbackEventReference (This, "Arg", "receivedatafromserver", "context");

String strcallback = "function Callbacktotheserver (arg, context) {" + Strrefrence + "};";

Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "Callbacktotheserver", Strcallback, True); Register JS function Callbacktotheserver
}
}

private string Strtimeformat;

public string GetCallbackResult ()
{
if (Strtimeformat! = "" && Strtimeformat = = "12")
{return DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss (12 Hour system)");}
Else
{return DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss (24 hour system)");}
}

public void RaiseCallbackEvent (String eventargument)
{
Strtimeformat = eventargument;
}
}


Default.aspx
<%@ page language= "C #" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>CallBack</title>
<script language= "javascript" type= "Text/javascript" >
function Receivedatafromserver (valuereturnfromserver,context)
{
Document.getelementbyidx ("Servertime"). InnerHTML = Valuereturnfromserver;
Document.getelementbyidx ("PIG"). InnerHTML = context;
}

function getservertime (format)
{callbacktotheserver (format, "You are a Pig");} Call the Callbacktotheserver function that triggers getcallbackeventreference (the JS function written on the server side)
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "BtnShow12" runat= "server" text= "Get Server Time (12 hour system)"OnClientClick= "Javascript:getservertime", return false; "/><br/>
<asp:button id= "btnShow24" runat= "server" text= "Get server Time (24 hour system)"OnClientClick= "Javascript:getservertime"; return false; "/><br/>
<br/>
<span id= "Servertime" ><%= DateTime.Now.ToString ("Yyyy-mm-dd HHHH:mm:ss")%></span> not clicked is the default display
<span id= "PIG" ></span>
</div>
</form>
</body>

Local refresh of server-side controls, CallBack

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: info-contact@alibabacloud.com 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.