Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ClientScriptManager.GetCallbackEventReference Method (String, String, String, String, String, Boolean)

Obtains a reference to a client function that, when invoked, initiates a client call back to server events. The client function for this overloaded method includes a specified target, argument, client script, context, error handler, and Boolean value.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public string GetCallbackEventReference(
	string target,
	string argument,
	string clientCallback,
	string context,
	string clientErrorCallback,
	bool useAsync
)

Parameters

target
Type: System.String

The name of a server Control that handles the client callback. The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent method.

argument
Type: System.String

An argument passed from the client script to the server

RaiseCallbackEvent method.

clientCallback
Type: System.String

The name of the client event handler that receives the result of the successful server event.

context
Type: System.String

The client script that is evaluated on the client prior to initiating the callback. The result of the script is passed back to the client event handler.

clientErrorCallback
Type: System.String

The name of the client event handler that receives the result when an error occurs in the server event handler.

useAsync
Type: System.Boolean

true to perform the callback asynchronously; false to perform the callback synchronously.

Return Value

Type: System.String
The name of a client function that invokes the client callback.

This overload of the GetCallbackEventReference method takes a target string parameter instead of a Control parameter. Use this overload when you want the callback to go back to something other than a string containing the UniqueID of the control.

Additionally, this overload of the GetCallbackEventReference method requires a useAsync and a clientErrorCallback parameter. The useAsync parameter allows you to perform the client callback asynchronously by setting the value to true. The overload versions of this method that do not require the useAsync parameter set the value to false by default. The clientErrorCallback parameter allows you to define the name of the client function that is called if the server handler, the RaiseCallbackEvent method, returns an error. The overload versions of this method that do not require the clientErrorCallback parameter set the value to null.

For more information on this method, see the remarks for the overload GetCallbackEventReference method.

The following code example demonstrates how to use two overloads of the GetCallbackEventReference method in a client callback scenario that increments integers.

Two callback mechanisms are shown; the difference between them is the use of the context parameter. A ReceiveServerData1 client callback function is provided using the context parameter. In contrast, the ReceiveServerData2 client callback function is defined in a <script> block on the page. A RaiseCallbackEvent method is the server handler that increments the value that is passed to it and the GetCallbackResult method returns the incremented value as a string. If the RaiseCallbackEvent method returns an error, then the client function ProcessCallBackError is called.

<%@ Page Language="C#" %>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    public int cbCount = 0;

    // Define method that processes the callbacks on server.
    public void RaiseCallbackEvent(String eventArgument)
    {
        cbCount = Convert.ToInt32(eventArgument) + 1;
    }

    // Define method that returns callback result.
    public string GetCallbackResult()
    {
        return cbCount.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Define a StringBuilder to hold messages to output.
        StringBuilder sb = new StringBuilder();

        // Check if this is a postback.
        sb.Append("No page postbacks have occurred.");
        if (Page.IsPostBack)
        {
            sb.Append("A page postback has occurred.");
        }

        // Write out any messages.
        MyLabel.Text = sb.ToString();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Define one of the callback script's context.
        // The callback script will be defined in a script block on the page.
        StringBuilder context1 = new StringBuilder();
        context1.Append("function ReceiveServerData1(arg, context)");
        context1.Append("{");
        context1.Append("Message1.innerText =  arg;");
        context1.Append("value1 = arg;");
        context1.Append("}");

        // Define callback references.
        String cbReference1 = cs.GetCallbackEventReference(this, "arg", 
            "ReceiveServerData1", context1.ToString());
        String cbReference2 = cs.GetCallbackEventReference("'" + 
            Page.UniqueID + "'", "arg", "ReceiveServerData2", "", 
            "ProcessCallBackError", false);
        String callbackScript1 = "function CallTheServer1(arg, context) {" + 
            cbReference1 + "; }";
        String callbackScript2 = "function CallTheServer2(arg, context) {" + 
            cbReference2 + "; }";

        // Register script blocks will perform call to the server.
        cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", 
            callbackScript1, true);
        cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", 
            callbackScript2, true);

    }
</script>

<script type="text/javascript">
var value1 = 0;
var value2 = 0;
function ReceiveServerData2(arg, context)
{
    Message2.innerText = arg;
    value2 = arg;
}
function ProcessCallBackError(arg, context)
{
    Message2.innerText = 'An error has occurred.';
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ClientScriptManager Example</title>
</head>
<body>
    <form id="Form1" 
          runat="server">
    <div>
      Callback 1 result: <span id="Message1">0</span>
      <br />
      Callback 2 result: <span id="Message2">0</span>
      <br /> <br />
      <input type="button" 
             value="ClientCallBack1" 
             onclick="CallTheServer1(value1, alert('Increment value'))"/>    
      <input type="button" 
             value="ClientCallBack2" 
             onclick="CallTheServer2(value2, alert('Increment value'))"/>
      <br /> <br />
      <asp:Label id="MyLabel" 
                 runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.