Breyta

Deila með


ScriptManager.RegisterDataItem Method

Definition

Sends custom data to controls during partial-page rendering.

Overloads

RegisterDataItem(Control, String)

Sends custom data to a control during partial-page rendering.

RegisterDataItem(Control, String, Boolean)

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)

Parameters

control
Control

The control that is receiving the data.

dataItem
String

The data that is sent to the control.

Exceptions

control is null.

dataItem is already registered for control.

Examples

The following example shows how to send data to two Label controls on a page during an asynchronous postback. The Label controls are not inside an UpdatePanel control.

Note

The data that is sent in this example is for illustration only. 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>

Remarks

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.

The RegisterDataItem method can be called only during an asynchronous postback. To determine whether a postback is asynchronous, use the IsInAsyncPostBack property. This method invokes the overload that takes a parameter named isJsonSerialized that is set to false. When the isJsonSerialized parameter is set to false, the string is not serialized as JavaScript Object Notation (JSON). For more information about the JSON format, see the Introducing JSON Web site.

The 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. For example, if you provide a handler for the pageLoading event, the custom data is passed in the PageLoadingEventArgs class, which exposes a dataItems property.

See also

Applies to

RegisterDataItem(Control, String, Boolean)

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)

Parameters

control
Control

The page control that is receiving the data.

dataItem
String

The data that is sent to the control.

isJsonSerialized
Boolean

true to indicate that dataItem is serialized as JSON; otherwise, false.

Exceptions

control is null.

dataItem is already registered for control.

Examples

The following example shows how to send data to two Label controls on a page during an asynchronous postback. The Label controls are not inside an UpdatePanel control. This example shows the overload that does not take the isJsonSerialized parameter. Otherwise, the procedure for retrieving the dataItems property of the PageLoadingEventArgs object is the same as if you did not use that overload.

Note

The data that is sent in this example is for illustration only. In a real-world application, you would use the RegisterDataItem method to send custom data from the server. For 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>

Remarks

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

If the dataItem parameter that you register for control is not serialized as JSON, set the isJsonSerialized parameter to false. This avoids the need to use the eval function for each string that is sent to the client. For more information about the JSON format, see the Introducing JSON Web site.

The RegisterDataItem method can be called only during an asynchronous postback. To determine whether a postback is asynchronous, use the IsInAsyncPostBack property.

The 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. For example, if you provide a handler for the pageLoading event, the custom data is passed in the PageLoadingEventArgs class, which exposes a dataItems property.

See also

Applies to