ClientScriptManager.GetCallbackEventReference 方法

定义

获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。Obtains a reference to a client function that, when invoked, initiates a client call back to a server event.

重载

GetCallbackEventReference(Control, String, String, String)

获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。Obtains a reference to a client function that, when invoked, initiates a client call back to a server event. 此重载方法的客户端函数包含指定的控件、参数、客户端脚本和上下文。The client function for this overloaded method includes a specified control, argument, client script, and context.

GetCallbackEventReference(Control, 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 control, argument, client script, context, and Boolean value.

GetCallbackEventReference(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.

GetCallbackEventReference(Control, 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 control, argument, client script, context, error handler, and Boolean value.

GetCallbackEventReference(Control, String, String, String)

获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。Obtains a reference to a client function that, when invoked, initiates a client call back to a server event. 此重载方法的客户端函数包含指定的控件、参数、客户端脚本和上下文。The client function for this overloaded method includes a specified control, argument, client script, and context.

public:
 System::String ^ GetCallbackEventReference(System::Web::UI::Control ^ control, System::String ^ argument, System::String ^ clientCallback, System::String ^ context);
public string GetCallbackEventReference (System.Web.UI.Control control, string argument, string clientCallback, string context);
member this.GetCallbackEventReference : System.Web.UI.Control * string * string * string -> string

参数

control
Control

处理客户端回调的服务器 ControlThe server Control that handles the client callback. 该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent(String) 方法。The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent(String) method.

argument
String

从客户端脚本传递给服务器的一个参数An argument passed from the client script to the server RaiseCallbackEvent(String) 方法。RaiseCallbackEvent(String) method.

clientCallback
String

一个客户端事件处理程序的名称,该处理程序接收成功的服务器事件的结果。The name of the client event handler that receives the result of the successful server event.

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

返回

调用客户端回调的客户端函数的名称。The name of a client function that invokes the client callback.

异常

指定的 ControlnullThe Control specified is null.

指定的 Control 未实现 ICallbackEventHandler 接口。The Control specified does not implement the ICallbackEventHandler interface.

示例

下面的代码示例演示如何在递增整数的客户端GetCallbackEventReference回调方案中使用方法的两个重载。The following code example demonstrates how to use two overloads of the GetCallbackEventReference method in a client callback scenario that increments integers.

将显示两个回调机制:它们之间的区别在于使用context参数。Two callback mechanisms are shown; the difference between them is the use of the context parameter. 使用参数提供ReceiveServerData1客户端回调函数。 contextA ReceiveServerData1 client callback function is provided using the context parameter. 相反, ReceiveServerData2客户端回调函数<script>在页的块中定义。In contrast, the ReceiveServerData2 client callback function is defined in a <script> block on the page. 方法是增加传递给它的值GetCallbackResult并且方法以字符串形式返回递增值的服务器处理程序。 RaiseCallbackEventA 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. 如果该RaiseCallbackEvent方法返回错误, ProcessCallBackError则调用客户端函数。If the RaiseCallbackEvent method returns an error, then the ProcessCallBackError client function 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>
<%@ Page Language="VB" %>
<%@ 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 cbCount As Integer = 0
    
    ' Define method that processes the callbacks on server.
    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
    Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        
        cbCount = Convert.ToInt32(eventArgument) + 1
        
    End Sub

    ' Define method that returns callback result.
    Public Function GetCallbackResult() _
    As String Implements _
    System.Web.UI.ICallbackEventHandler.GetCallbackResult

        Return cbCount.ToString()
        
    End Function
    
    
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
        ' Define a StringBuilder to hold messages to output.
        Dim sb As New StringBuilder()
    
        ' Check if this is a postback.
        sb.Append("No page postbacks have occurred.")
        If (Page.IsPostBack) Then
      
            sb.Append("A page postback has occurred.")
      
        End If
    
        ' Write out any messages.
        MyLabel.Text = sb.ToString()
    
        ' Get a ClientScriptManager reference from the Page class.
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Define one of the callback script's context.
        ' The callback script will be defined in a script block on the page.
        Dim context1 As New StringBuilder()
        context1.Append("function ReceiveServerData1(arg, context)")
        context1.Append("{")
        context1.Append("Message1.innerText =  arg;")
        context1.Append("value1 = arg;")
        context1.Append("}")
    
        ' Define callback references.
        Dim cbReference1 As String = cs.GetCallbackEventReference(Me, "arg", _
            "ReceiveServerData1", context1.ToString())
        Dim cbReference2 As String = cs.GetCallbackEventReference("'" & _
            Page.UniqueID & "'", "arg", "ReceiveServerData2", "", "ProcessCallBackError", False)
        Dim callbackScript1 As String = "function CallTheServer1(arg, context) {" + _
            cbReference1 + "; }"
        Dim callbackScript2 As String = "function CallTheServer2(arg, context) {" + _
            cbReference2 + "; }"
    
        ' Register script blocks will perform call to the server.
        cs.RegisterClientScriptBlock(Me.GetType(), "CallTheServer1", _
            callbackScript1, True)
        cs.RegisterClientScriptBlock(Me.GetType(), "CallTheServer2", _
            callbackScript2, True)
    
    End Sub
</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>
    <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>

注解

GetCallbackEventReference(Control, String, String, String)方法执行对服务器的带外回调, 该服务器是页的正常生命周期的修改版本。The GetCallbackEventReference(Control, String, String, String) method performs an out-of-band callback to the server that is a modified version of a page's normal life cycle. 有关详细信息, 请参阅实现无回发的客户端回调For more information, see Implementing Client Callbacks Without Postbacks.

备注

当浏览器是 Microsoft Internet Explorer (5.0 版或更高版本) 时, 脚本回调机制是通过 Microsoft XmlHttp COM 对象实现的, 并且需要将浏览器设置为运行 ActiveX 控件。When the browser is Microsoft Internet Explorer (version 5.0 or later), the script callback mechanism is implemented through the Microsoft.XmlHttp COM object and requires the browser to be set to run ActiveX controls. 对于其他浏览器, 使用浏览器的本地文档对象模型 (DOM) 的 XMLHttpRequest。For other browsers, an XMLHttpRequest using the browser's local Document Object Model (DOM) is used. 若要检查浏览器是否支持客户端回调, SupportsCallback请使用属性。To check whether a browser supports client callbacks, use the SupportsCallback property. 若要检查浏览器是否支持 XML over HTTP, 请SupportsXmlHttp使用属性。To check whether a browser supports XML over HTTP, use the SupportsXmlHttp property. 这两个属性都可以Browser通过内部 ASP.NET Request对象的属性进行访问。Both properties are accessible through the Browser property of the intrinsic ASP.NET Request object.

GetCallbackEventReference方法GetCallbackEventReference的重载使用 XML over HTTP 来同步执行回调。The GetCallbackEventReference overload of the GetCallbackEventReference method performs a callback synchronously using XML over HTTP. 在回调方案中同步发送数据时, 同步回调会立即返回, 而不会阻止浏览器。When sending data synchronously in a callback scenario, synchronous callbacks return immediately and do not block the browser. 在浏览器中, 不能同时执行两个同步回调回调。No two synchronous callbacks callback can execute at the same time in the browser. 如果在当前挂起一个同步回调时激发另一个同步回调, 则第二个同步回调将取消第一个回调, 并且只有第二个回调返回。If a second synchronous callback is fired while one is currently pending, the second synchronous callback cancels the first and only the second callback will return.

若要以异步方式发送数据, 请使用采用useAsync参数的重载之一, 这是控制此行为的布尔值。To send data asynchronously, use one of the overloads that takes the useAsync parameter, which is a Boolean value controlling this behavior. 在异步方案中, 可以有多个挂起的回调;但是, 不能保证它们返回的顺序与启动顺序一致。In the asynchronous scenario you can have multiple pending callbacks; however, the order in which they return is not guaranteed to match the order in which they were initiated.

此外, 此GetCallbackEventReference方法的重载指定无客户端函数来处理RaiseCallbackEvent方法生成的错误条件。Additionally, this overload of the GetCallbackEventReference method specifies no client function to handle the case of an error condition generated by the RaiseCallbackEvent method. 若要指定客户端错误回调处理程序, 请使用采用clientErrorCallback参数的重载之一。To specify a client error callback handler, use one of the overloads that takes the clientErrorCallback parameter.

方法采用一个可选的字符串argument参数并返回一个字符串。 GetCallbackEventReference(Control, String, String, String)The GetCallbackEventReference(Control, String, String, String) method takes an optional string argument parameter and returns a string. 若要传入或接收多个值, 请分别连接输入或返回字符串中的值。To pass in or to receive multiple values, concatenate values in the input or return string, respectively.

备注

避免在脚本回调操作期间需要更新的页或控件属性的实现中使用视图状态。Avoid using the view state in the implementation of page or control properties that need be updated during script callback operations. 如果属性在页面请求后仍然存在, 则可以使用会话状态。If the properties are to survive page requests, you can use session state.

另请参阅

GetCallbackEventReference(Control, 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 control, argument, client script, context, and Boolean value.

public:
 System::String ^ GetCallbackEventReference(System::Web::UI::Control ^ control, System::String ^ argument, System::String ^ clientCallback, System::String ^ context, bool useAsync);
public string GetCallbackEventReference (System.Web.UI.Control control, string argument, string clientCallback, string context, bool useAsync);
member this.GetCallbackEventReference : System.Web.UI.Control * string * string * string * bool -> string

参数

control
Control

处理客户端回调的服务器 ControlThe server Control that handles the client callback. 该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent(String) 方法。The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent(String) method.

argument
String

从客户端脚本传递给服务器的一个参数An argument passed from the client script to the server RaiseCallbackEvent(String) 方法。RaiseCallbackEvent(String) method.

clientCallback
String

一个客户端事件处理程序的名称,该处理程序接收成功的服务器事件的结果。The name of the client event handler that receives the result of the successful server event.

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

useAsync
Boolean

true 表示异步执行回调;false 表示同步执行回调。true to perform the callback asynchronously; false to perform the callback synchronously.

返回

调用客户端回调的客户端函数的名称。The name of a client function that invokes the client callback.

异常

指定的 ControlnullThe Control specified is null.

指定的 Control 未实现 ICallbackEventHandler 接口。The Control specified does not implement the ICallbackEventHandler interface.

注解

GetCallbackEventReference方法重载需要一个useAsync参数, 该参数允许您通过将值设置为true, 以异步方式执行客户端回调。This overload of the GetCallbackEventReference method requires a useAsync parameter, which allows you to perform the client callback asynchronously by setting the value to true. 默认情况下, 此方法的重载版本不需要useAsync参数将值设置为falseThe overload versions of this method that do not require the useAsync parameter set the value to false by default.

有关此方法的详细信息, 请参阅重载GetCallbackEventReference方法的备注。For more information on this method, see the remarks for the overload GetCallbackEventReference method.

另请参阅

GetCallbackEventReference(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.

public:
 System::String ^ GetCallbackEventReference(System::String ^ target, System::String ^ argument, System::String ^ clientCallback, System::String ^ context, System::String ^ clientErrorCallback, bool useAsync);
public string GetCallbackEventReference (string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync);
member this.GetCallbackEventReference : string * string * string * string * string * bool -> string
Public Function GetCallbackEventReference (target As String, argument As String, clientCallback As String, context As String, clientErrorCallback As String, useAsync As Boolean) As String

参数

target
String

处理客户端回调的服务器 Control 的名称。The name of a server Control that handles the client callback. 该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent(String) 方法。The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent(String) method.

argument
String

从客户端脚本传递给服务器的一个参数An argument passed from the client script to the server RaiseCallbackEvent(String) 方法。RaiseCallbackEvent(String) method.

clientCallback
String

一个客户端事件处理程序的名称,该处理程序接收成功的服务器事件的结果。The name of the client event handler that receives the result of the successful server event.

context
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
String

客户端事件处理程序的名称,该处理程序在服务器端事件处理程序出现错误时接收结果。The name of the client event handler that receives the result when an error occurs in the server event handler.

useAsync
Boolean

true 表示异步执行回调;false 表示同步执行回调。true to perform the callback asynchronously; false to perform the callback synchronously.

返回

调用客户端回调的客户端函数的名称。The name of a client function that invokes the client callback.

示例

下面的代码示例演示如何在递增整数的客户端GetCallbackEventReference回调方案中使用方法的两个重载。The following code example demonstrates how to use two overloads of the GetCallbackEventReference method in a client callback scenario that increments integers.

将显示两个回调机制:它们之间的区别在于使用context参数。Two callback mechanisms are shown; the difference between them is the use of the context parameter. 使用参数提供ReceiveServerData1客户端回调函数。 contextA ReceiveServerData1 client callback function is provided using the context parameter. 相反, ReceiveServerData2客户端回调函数<script>在页的块中定义。In contrast, the ReceiveServerData2 client callback function is defined in a <script> block on the page. 方法是增加传递给它的值GetCallbackResult并且方法以字符串形式返回递增值的服务器处理程序。 RaiseCallbackEventA 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. 如果该RaiseCallbackEvent方法返回错误, 则调用客户端函数ProcessCallBackErrorIf 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>
<%@ Page Language="VB" %>
<%@ 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 cbCount As Integer = 0
    
    ' Define method that processes the callbacks on server.
    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
    Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        
        cbCount = Convert.ToInt32(eventArgument) + 1
        
    End Sub

    ' Define method that returns callback result.
    Public Function GetCallbackResult() _
    As String Implements _
    System.Web.UI.ICallbackEventHandler.GetCallbackResult

        Return cbCount.ToString()
        
    End Function
    
    
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
        ' Define a StringBuilder to hold messages to output.
        Dim sb As New StringBuilder()
    
        ' Check if this is a postback.
        sb.Append("No page postbacks have occurred.")
        If (Page.IsPostBack) Then
      
            sb.Append("A page postback has occurred.")
      
        End If
    
        ' Write out any messages.
        MyLabel.Text = sb.ToString()
    
        ' Get a ClientScriptManager reference from the Page class.
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Define one of the callback script's context.
        ' The callback script will be defined in a script block on the page.
        Dim context1 As New StringBuilder()
        context1.Append("function ReceiveServerData1(arg, context)")
        context1.Append("{")
        context1.Append("Message1.innerText =  arg;")
        context1.Append("value1 = arg;")
        context1.Append("}")
    
        ' Define callback references.
        Dim cbReference1 As String = cs.GetCallbackEventReference(Me, "arg", _
            "ReceiveServerData1", context1.ToString())
        Dim cbReference2 As String = cs.GetCallbackEventReference("'" & _
            Page.UniqueID & "'", "arg", "ReceiveServerData2", "", "ProcessCallBackError", False)
        Dim callbackScript1 As String = "function CallTheServer1(arg, context) {" + _
            cbReference1 + "; }"
        Dim callbackScript2 As String = "function CallTheServer2(arg, context) {" + _
            cbReference2 + "; }"
    
        ' Register script blocks will perform call to the server.
        cs.RegisterClientScriptBlock(Me.GetType(), "CallTheServer1", _
            callbackScript1, True)
        cs.RegisterClientScriptBlock(Me.GetType(), "CallTheServer2", _
            callbackScript2, True)
    
    End Sub
</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>
    <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>

注解

GetCallbackEventReference方法的重载target采用Control字符串参数, 而不是参数。This overload of the GetCallbackEventReference method takes a target string parameter instead of a Control parameter. 如果希望回调返回到包含UniqueID控件的的字符串以外的其他内容, 请使用此重载。Use this overload when you want the callback to go back to something other than a string containing the UniqueID of the control.

此外, 此GetCallbackEventReference方法重载useAsync需要和clientErrorCallback参数。Additionally, this overload of the GetCallbackEventReference method requires a useAsync and a clientErrorCallback parameter. 参数允许您通过将值设置为true, 以异步方式执行客户端回调。 useAsyncThe useAsync parameter allows you to perform the client callback asynchronously by setting the value to true. 默认情况下, 此方法的重载版本不需要useAsync参数将值设置为falseThe overload versions of this method that do not require the useAsync parameter set the value to false by default. 参数允许您定义在服务器处理程序RaiseCallbackEvent (方法) 返回错误的情况下调用的客户端函数的名称。 clientErrorCallbackThe 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. 此方法的重载版本不需要clientErrorCallback参数将值设置为 null。The overload versions of this method that do not require the clientErrorCallback parameter set the value to null.

有关此方法的详细信息, 请参阅重载GetCallbackEventReference方法的备注。For more information on this method, see the remarks for the overload GetCallbackEventReference method.

另请参阅

GetCallbackEventReference(Control, 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 control, argument, client script, context, error handler, and Boolean value.

public:
 System::String ^ GetCallbackEventReference(System::Web::UI::Control ^ control, System::String ^ argument, System::String ^ clientCallback, System::String ^ context, System::String ^ clientErrorCallback, bool useAsync);
public string GetCallbackEventReference (System.Web.UI.Control control, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync);
member this.GetCallbackEventReference : System.Web.UI.Control * string * string * string * string * bool -> string

参数

control
Control

处理客户端回调的服务器 ControlThe server Control that handles the client callback. 该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent(String) 方法。The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent(String) method.

argument
String

从客户端脚本传递给服务器的 RaiseCallbackEvent(String) 方法的参数。An argument passed from the client script to the server RaiseCallbackEvent(String) method.

clientCallback
String

一个客户端事件处理程序的名称,该处理程序接收成功的服务器事件的结果。The name of the client event handler that receives the result of the successful server event.

context
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
String

客户端事件处理程序的名称,该处理程序在服务器端事件处理程序出现错误时接收结果。The name of the client event handler that receives the result when an error occurs in the server event handler.

useAsync
Boolean

true 表示异步执行回调;false 表示同步执行回调。true to perform the callback asynchronously; false to perform the callback synchronously.

返回

调用客户端回调的客户端函数的名称。The name of a client function that invokes the client callback.

异常

指定的 ControlnullThe Control specified is null.

指定的 Control 未实现 ICallbackEventHandler 接口。The Control specified does not implement the ICallbackEventHandler interface.

注解

GetCallbackEventReference方法重载useAsync需要和clientErrorCallback参数。This overload of the GetCallbackEventReference method requires a useAsync and a clientErrorCallback parameter. 参数允许您通过将值设置为true, 以异步方式执行客户端回调。 useAsyncThe useAsync parameter allows you to perform the client callback asynchronously by setting the value to true. 默认情况下, 此方法的重载版本不需要useAsync参数将值设置为falseThe overload versions of this method that do not require the useAsync parameter set the value to false by default. 参数用于定义客户端函数的名称, 如果服务器处理程序RaiseCallbackEvent (方法) 返回错误, 则调用该函数。 clientErrorCallbackThe 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. 此方法的重载版本不需要clientErrorCallback参数将值设置为 null。The overload versions of this method that do not require the clientErrorCallback parameter set the value to null.

有关此方法的详细信息, 请参阅重载GetCallbackEventReference方法的备注。For more information on this method, see the remarks for the overload GetCallbackEventReference method.

另请参阅

适用于