ScriptManager.RegisterDataItem 方法

定义

在局部页面呈现期间将自定义数据发送到多个控件。Sends custom data to controls during partial-page rendering.

重载

RegisterDataItem(Control, String)

在局部页面呈现期间将自定义数据发送到某个控件。Sends custom data to a control during partial-page rendering.

RegisterDataItem(Control, String, Boolean)

在局部页面呈现期间,将自定义数据发送到某个控件,并指示数据是否为 JavaScript 对象表示法 (JSON) 格式。Sends custom data to a control during partial-page rendering, and indicates whether the data is in JavaScript Object Notation (JSON) format.

RegisterDataItem(Control, String)

在局部页面呈现期间将自定义数据发送到某个控件。Sends custom data to a control during partial-page rendering.

public:
 void RegisterDataItem(System::Web::UI::Control ^ control, System::String ^ dataItem);
public void RegisterDataItem (System.Web.UI.Control control, string dataItem);
member this.RegisterDataItem : System.Web.UI.Control * string -> unit
Public Sub RegisterDataItem (control As Control, dataItem As String)

参数

control
Control

正在接收数据的控件。The control that is receiving the data.

dataItem
String

发送到控件的数据。The data that is sent to the control.

例外

controlnullcontrol is null.

在回发期间调用 RegisterDataItem(Control, String, Boolean) 方法。The RegisterDataItem(Control, String, Boolean) method is called during a postback.

已为 control 注册 dataItemdataItem is already registered for control.

示例

下面的示例演示如何在 Label 异步回发期间将数据发送到页面上的两个控件。The following example shows how to send data to two Label controls on a page during an asynchronous postback. Label控件不在 UpdatePanel 控件内。The Label controls are not inside an UpdatePanel control.

备注

在此示例中发送的数据仅用于说明。The data that is sent in this example is for illustration only. 在实际应用程序中,可以使用 RegisterDataItem 方法从服务器发送自定义数据。In a real-world application, you would use the RegisterDataItem method to send custom data from the server.

<%@ Page Language="C#" %>

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

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ScriptManager1.IsInAsyncPostBack)
        {
            System.Web.Script.Serialization.JavaScriptSerializer json =
                new System.Web.Script.Serialization.JavaScriptSerializer();
            ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString());
            ScriptManager1.RegisterDataItem(Label2, json.Serialize("more data"), true);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ScriptManager RegisterDataItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript" language="javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
        function PageLoadingHandler(sender, args) {
            var dataItems = args.get_dataItems();
            if ($get('Label1') !== null)
                $get('Label1').innerHTML = dataItems['Label1'];
            if ($get('Label2') !== null)
                $get('Label2').innerHTML = dataItems['Label2'];
        }
    </script>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
       UpdatePanel content.
       <asp:Button ID="Button1" Text="Submit" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
    <hr />
    <asp:Label ID="Label1" runat="server" /> <br />
    <asp:Label ID="Label2" runat="server" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

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

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If (ScriptManager1.IsInAsyncPostBack) Then
            Dim json As New System.Web.Script.Serialization.JavaScriptSerializer
            ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString())
            ScriptManager1.RegisterDataItem(Label2, json.Serialize("more data"), True)
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ScriptManager RegisterDataItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript" language="javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
        function PageLoadingHandler(sender, args) {
            var dataItems = args.get_dataItems();
            if ($get('Label1') !== null)
                $get('Label1').innerHTML = dataItems['Label1'];
            if ($get('Label2') !== null)
                $get('Label2').innerHTML = dataItems['Label2'];
        }
    </script>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
       UpdatePanel content.
       <asp:Button ID="Button1" Text="Submit" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
    <hr />
    <asp:Label ID="Label1" runat="server" /> <br />
    <asp:Label ID="Label2" runat="server" />
    </div>
    </form>
</body>
</html>

注解

RegisterDataItem 异步回发过程中,使用方法将数据从服务器发送到客户端,而不管接收数据的控件是否在 UpdatePanel 控件中。Use the RegisterDataItem method to send data from the server to the client during asynchronous postbacks, regardless of whether the control receiving the data is inside an UpdatePanel control.

RegisterDataItem只能在异步回发期间调用方法。The RegisterDataItem method can be called only during an asynchronous postback. 若要确定回发是否是异步的,请使用 IsInAsyncPostBack 属性。To determine whether a postback is asynchronous, use the IsInAsyncPostBack property. 此方法调用重载,该重载采用一个名为的参数,该参数 isJsonSerialized 设置为 falseThis method invokes the overload that takes a parameter named isJsonSerialized that is set to false. isJsonSerialized 参数设置为时 false ,不会将该字符串序列化为 (JSON) JavaScript 对象表示法。When the isJsonSerialized parameter is set to false, the string is not serialized as JavaScript Object Notation (JSON). 有关 JSON 格式的详细信息,请参阅 json 网站简介For more information about the JSON format, see the Introducing JSON Web site.

RegisterDataItem pageLoading 对象的、和事件期间,可以在客户端脚本中访问注册到方法的数据项 pageLoaded endRequest PageRequestManagerThe data items that are registered with the RegisterDataItem method can be accessed in client script during the pageLoading, pageLoaded, and endRequest events of the PageRequestManager object. 处理这些事件时,将在事件参数对象中传递自定义数据。When you handle these events, the custom data is passed in an event argument object. 例如,如果为事件提供处理程序,则 pageLoading 会在类中传递自定义数据 PageLoadingEventArgs ,该类将公开一个属性。For example, if you provide a handler for the pageLoading event, the custom data is passed in the PageLoadingEventArgs class, which exposes a property.

另请参阅

适用于

RegisterDataItem(Control, String, Boolean)

在局部页面呈现期间,将自定义数据发送到某个控件,并指示数据是否为 JavaScript 对象表示法 (JSON) 格式。Sends custom data to a control during partial-page rendering, and indicates whether the data is in JavaScript Object Notation (JSON) format.

public:
 void RegisterDataItem(System::Web::UI::Control ^ control, System::String ^ dataItem, bool isJsonSerialized);
public void RegisterDataItem (System.Web.UI.Control control, string dataItem, bool isJsonSerialized);
member this.RegisterDataItem : System.Web.UI.Control * string * bool -> unit
Public Sub RegisterDataItem (control As Control, dataItem As String, isJsonSerialized As Boolean)

参数

control
Control

正在接收数据的页控件。The page control that is receiving the data.

dataItem
String

发送到控件的数据。The data that is sent to the control.

isJsonSerialized
Boolean

如果要将 dataItem 序列化为 JSON,则为 true;否则为 falsetrue to indicate that dataItem is serialized as JSON; otherwise, false.

例外

controlnullcontrol is null.

在回发期间调用 RegisterDataItem(Control, String, Boolean) 方法。The RegisterDataItem(Control, String, Boolean) method is called during a postback.

已为 control 注册 dataItemdataItem is already registered for control.

示例

下面的示例演示如何在 Label 异步回发期间将数据发送到页面上的两个控件。The following example shows how to send data to two Label controls on a page during an asynchronous postback. Label控件不在 UpdatePanel 控件内。The Label controls are not inside an UpdatePanel control. 此示例显示不采用参数的重载 isJsonSerializedThis example shows the overload that does not take the isJsonSerialized parameter. 否则,检索对象的属性的过程与 dataItems PageLoadingEventArgs 不使用该重载时相同。Otherwise, the procedure for retrieving the dataItems property of the PageLoadingEventArgs object is the same as if you did not use that overload.

备注

在此示例中发送的数据仅用于说明。The data that is sent in this example is for illustration only. 在实际应用程序中,可以使用 RegisterDataItem 方法从服务器发送自定义数据。In a real-world application, you would use the RegisterDataItem method to send custom data from the server. 例如,您可以使用数据项发送有关是否隐藏或显示不在控件内的客户端元素的信息 UpdatePanelFor example, you could use the data item to send information about whether to hide or show client elements that are not inside an UpdatePanel control.

<%@ Page Language="C#" %>

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

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ScriptManager1.IsInAsyncPostBack)
        {
            System.Web.Script.Serialization.JavaScriptSerializer json =
                new System.Web.Script.Serialization.JavaScriptSerializer();
            ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString());
            ScriptManager1.RegisterDataItem(Label2, json.Serialize("more data"), true);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ScriptManager RegisterDataItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript" language="javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
        function PageLoadingHandler(sender, args) {
            var dataItems = args.get_dataItems();
            if ($get('Label1') !== null)
                $get('Label1').innerHTML = dataItems['Label1'];
            if ($get('Label2') !== null)
                $get('Label2').innerHTML = dataItems['Label2'];
        }
    </script>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
       UpdatePanel content.
       <asp:Button ID="Button1" Text="Submit" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
    <hr />
    <asp:Label ID="Label1" runat="server" /> <br />
    <asp:Label ID="Label2" runat="server" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

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

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If (ScriptManager1.IsInAsyncPostBack) Then
            Dim json As New System.Web.Script.Serialization.JavaScriptSerializer
            ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString())
            ScriptManager1.RegisterDataItem(Label2, json.Serialize("more data"), True)
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ScriptManager RegisterDataItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript" language="javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
        function PageLoadingHandler(sender, args) {
            var dataItems = args.get_dataItems();
            if ($get('Label1') !== null)
                $get('Label1').innerHTML = dataItems['Label1'];
            if ($get('Label2') !== null)
                $get('Label2').innerHTML = dataItems['Label2'];
        }
    </script>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
       UpdatePanel content.
       <asp:Button ID="Button1" Text="Submit" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
    <hr />
    <asp:Label ID="Label1" runat="server" /> <br />
    <asp:Label ID="Label2" runat="server" />
    </div>
    </form>
</body>
</html>

注解

RegisterDataItem 异步回发过程中,使用方法将数据从服务器发送到客户端,而不管接收数据的控件是否在控件中 UpdatePanelYou use the RegisterDataItem method to send data from the server to the client during asynchronous postbacks, regardless of whether the control receiving the data is inside an UpdatePanel control.

如果 dataItem 为注册的参数 control 未序列化为 JSON,请将 isJsonSerialized 参数设置为 falseIf the dataItem parameter that you register for control is not serialized as JSON, set the isJsonSerialized parameter to false. 这就不必为 eval 发送到客户端的每个字符串使用函数。This avoids the need to use the eval function for each string that is sent to the client. 有关 JSON 格式的详细信息,请参阅 json 网站简介For more information about the JSON format, see the Introducing JSON Web site.

RegisterDataItem只能在异步回发期间调用方法。The RegisterDataItem method can be called only during an asynchronous postback. 若要确定回发是否是异步的,请使用 IsInAsyncPostBack 属性。To determine whether a postback is asynchronous, use the IsInAsyncPostBack property.

RegisterDataItem pageLoading 对象的、和事件期间,可以在客户端脚本中访问通过使用方法注册的数据项 pageLoaded endRequest PageRequestManagerThe data items that are registered by using the RegisterDataItem method can be accessed in client script during the pageLoading, pageLoaded, and endRequest events of the PageRequestManager object. 处理这些事件时,将在事件参数对象中传递自定义数据。When you handle these events, the custom data is passed in an event argument object. 例如,如果为事件提供处理程序,则 pageLoading 会在类中传递自定义数据 PageLoadingEventArgs ,该类将公开一个属性。For example, if you provide a handler for the pageLoading event, the custom data is passed in the PageLoadingEventArgs class, which exposes a property.

另请参阅

适用于