The GetCallbackEventReference function is new in the. NET Framework version 2.0. MSDN Explanation: Gets a reference to a client function that, when called, initiates a client callback to the server-side event. The client function for this overloaded method contains the specified controls, parameters, client script, and context.
Function Prototypes:
public string GetCallbackEventReference (
Control control,
String argument,
String Clientcallback,
String context
)
The first parameter specifies the target object on which the server side will process the callback, which is the server-side control that handles client callbacks. The control must implement the ICallbackEventHandler interface and provide the RaiseCallbackEvent method.
If this is passed in, the page itself is represented. You can also pass in any reference to a page control that implements ICallbackEventHandler.
In any case, the client's submit action (submit action) is submitted to the same ASPX page as the standard callback (postback) mechanism.
The second parameter is a JavaScript constant expression that represents the input data that the page passes to the server. If the value of the currently selected element in a drop-down list is passed in
"Document.getelementbyid[' cboemployees '].value".
The third parameter is a user-defined JavaScript callback (callback) function name in the <script> block. After the callback (callback) executes, this function is ultimately responsible for updating the page.
The last two optional parameters can be used to specify the error handling function (handler) and the context object.
The GetCallbackEventReference function will produce the following script call:
Webform_docallback (PageID, input, updateemployeeviewhandler, NULL, NULL);
The code for this function is automatically downloaded to the client and added to the page's response via the <script> tab (pages ' s response). Webform_docallback using XMLHttpRequest DOM objects
(XMLHttpRequest DOM Object) callback (call back) the URL of the current page.
At the same time, it adds some additional hidden fields (hidden field) so that the server distinguishes between simpler lightweight callback (callback) requests and general callbacks (postback) requests. When the request is processed, ASP. NET runtime (runtime) identifies the target object of the call (the first parameter passed to GetCallbackEventReference), Confirm that it implements the ICallbackEventHandler interface after calling the RaiseCallbackEvent method:
void RaiseCallbackEvent (
String eventargument
)
Eventargument is typically passed to the server-side input data via the page specified by GetCallbackEventReference, which is parameter 2.
RaiseCallbackEvent processing is done by calling the public string GetCallbackResult () to return the processing result to the calling control, The client-side script Clientcallback specified by the client executes GetCallbackEventReference, which completes the client refresh.
As stated above, script callbacks do not apply to all browsers, although the latest browsers, including Internet Explorer 5+, Netscape 6+, and safari1.2+, are callback.
Microsoft has added two new browser bapabilities:supportsxmlhttp and Supportscallback in ASP. NET 2.0 to enable developers to check the feasibility of the scenario.
A cautionary message to make the page refresh faster
Although script callbacks are defined in ASP. Callback 2.0, it is not very difficult to make them work in ASP. NET 1.1. in ASP. NET 2.0, many server controls provide faster page refreshes with scripting Callbacks (script callback).
Perhaps the most prominent example is the GridView control, which, as the successor to the DataGrid, selectively uses script callbacks (scripts callback) to display the paging record.
As previously mentioned, script callbacks (scripts callback) depend on the XMLHttpRequest object of the Document Object Model (DOM). In Internet Explorer, this Document Object model object (DOM object) implements--microsoft.xmlhttp through ActiveX controls (ActiveX control).
When browsing such a page in IE, you must appropriately reduce the security settings to allow ActiveX controls (ActiveX control) to be called by the script. This is not necessary in other browsers that implement the XMLHttpRequest Document Object model object (DOM object) in the same way.
In fact, Mozilla-based browsing has built-in support for the HTTP request function, instead of using ActiveX controls (ActiveX control)--a feature that is expected in Internet Explorer 7.0.
Example, click the button to get the result after server-side processing:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= " Getcallbackeventreferenceuse._default "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script type= "Text/javascript" >
Server-Side Call client method
function Clientreferenceserverdata (returnvalue) {
alert (returnvalue);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
Pass to server parameters:
<input id= "Txbserverparameter" type= "text"/>
<input id= "Btncallserver" type= "button" value= "CallServer" onclick= "Clientcallserver ()"/>
</div>
</form>
</body>
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Text;
Namespace Getcallbackeventreferenceuse
{
Public partial class _default:system.web.ui.page, ICallbackEventHandler
{
private string Callbackresult = String. Empty;
protected void Page_Load (object sender, EventArgs e)
{
Generating a method script for client-side invocation of the server
String reference = Page.ClientScript.GetCallbackEventReference (This,
"document.getElementById (' Txbserverparameter '). Value",
"Clientreferenceserverdata", "" ");
Client Invoke server-side method
String callbackscript = "function Clientcallserver ()" +
"{" +
Reference
+ ";}";
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "Clientcallserver", Callbackscript, True);
}
return results
public string GetCallbackResult ()
{
Return "Server Result:" + callbackresult;
}
Callback-Time Event
The eventargument value and the "document.getElementById (' Txbserverparameter ') in Page.ClientScript.GetCallbackEventReference. Value "corresponds
public void RaiseCallbackEvent (String eventargument)
{
Callbackresult = eventargument;
}
}
GetCallbackEventReference (client Invoke server-side) usage)