GetCallbackEventReference (the client calls the server) Usage

Source: Internet
Author: User
Tags call back
The GetCallbackEventReference function is added in. NET Framework 2.0.

 

MSDN explanation: gets a reference to a client function. When this function is called, a client callback for server-side events is started. The client function of this overload method contains the specified controls, parameters, client scripts, and context.

 

Function prototype:
Public string GetCallbackEventReference (
Control control,
String argument,
String clientCallback,
String context
)

The first parameter specifies the target object for the server to process the callback, that is, the server-side control for processing the client callback. This control must implement the ICallbackEventHandler interface and provide the RaiseCallbackEvent method.
If this is passed in, it indicates the page itself. You can also input a reference to any page control that implements ICallbackEventHandler.
Under any circumstances, the client's submit action will be submitted to the ASPX page with the same mechanism as the standard callback.
The second parameter is a JavaScript constant expression that indicates the input data sent from the page to the server. For example, input the value of the currently selected element in a drop-down list

"Document. getElementById ['cboemployees']. value ".
The third parameter is a user-defined JavaScript callback (callback) function name in the <script> block. After the callback is executed, this function is responsible for page updates.
The last two optional parameters can be used to specify the error handler and context object ).

 

The GetCallbackEventReference function generates the following script calls:

WebForm_DoCallback (pageID, input, UpdateEmployeeViewHandler, null, null );

The code of this function is automatically downloaded to the client and added to the page response by using the <script> tag. WebForm_DoCallback uses XmlHttpRequest DOM object

 

(XmlHttpRequest DOM object) call back the URL of the current page.
At the same time, it will add some additional hidden fields so that the server can distinguish between simpler lightweight callback requests and general callback requests. During request processing, ASP. NET runtime identifies the calling target object (the first parameter passed to GetCallbackEventReference) and confirms that it implements the ICallbackEventHandler interface and calls the RaiseCallbackEvent method:

Void RaiseCallbackEvent (
String eventArgument
)

EventArgument transmits the input data to the server through the page specified by GetCallbackEventReference, that is, parameter 2.

 

After RaiseCallbackEvent is processed, call public string GetCallbackResult () to return the processing result to the call control. The client executes the clientCallback script specified by GetCallbackEventReference to refresh the client.

As mentioned above, script callback is not applicable to all browsers, even though the latest browsers, including Internet Explorer 5 +, Netscape 6 +, and Safari1.2 +, can be used.
Microsoft added two new browser bapabilities in ASP. NET 2.0: SupportsXmlHttp and SupportsCallback so that developers can check the feasibility of the solution.

Warning for faster page refresh
Although script callback is defined in ASP. NET 2.0, it is not difficult to make it work in ASP. NET 1.1. In ASP. NET 2.0, many server controls use script callback to provide faster page refresh.
The most prominent example is the GridView control. As the successor of the DataGrid, it selectively uses the script callback to display records on pages.
As mentioned above, script callback depends 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 control.
When browsing such a page in IE, you must properly reduce the security settings to allow ActiveX control to be called by scripts. 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 browser has built-in support for the HTTP request feature, but does not use ActiveX control-this is also a high-hopes feature in Internet Explorer 7.0.

 

For example, click the button to get the server processing result:

<% @ 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script type = "text/javascript">
// Call the client on the server side
Function ClientReferenceServerData (returnValue ){
Alert (returnValue );
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
Parameters passed to the server:
<Input id = "txbServerParameter" type = "text"/>
<Input id = "btnCallServer" type = "button" value = "CallServer" onclick = "ClientCallServer ()"/>
</Div>
</Form>
</Body>
</Html>

 

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)
{
// Generate a method script for the client to call the server
String reference = Page. ClientScript. GetCallbackEventReference (this,
"Document. getElementById ('txbserverparameter '). value ",
"ClientReferenceServerData ","");

// The client calls the server Method
String callbackScript = "function ClientCallServer ()" +
"{" +
Reference
+ ";}";
Page. ClientScript. RegisterClientScriptBlock (this. GetType (), "ClientCallServer", callbackScript, true );
}

// Return results
Public string GetCallbackResult ()
{
Return "Server result:" + callBackResult;
}

// Callback the current event
// The eventArgument value corresponds to "document. getElementById ('txbserverparameter '). value" in Page. ClientScript. GetCallbackEventReference
Public void RaiseCallbackEvent (string eventArgument)
{
CallBackResult = eventArgument;
}

}

}

 

Reference: http://www.cnblogs.com/mywebname/articles/1060383.html
 

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