Export (0) Print
Expand All
Expand Minimize

ScriptManager.RegisterAsyncPostBackControl Method

Registers a control as a trigger for asynchronous postbacks.

Namespace:  System.Web.UI
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

'Declaration
Public Sub RegisterAsyncPostBackControl ( _
	control As Control _
)

Parameters

control
Type: System.Web.UI.Control

The control to register for asynchronous postbacks.

ExceptionCondition
ArgumentNullException

control is Nothing.

ArgumentException

The page is registered as an asynchronous postback control.

ArgumentException

The registered control does not implement the INamingContainer, IPostBackDataHandler, or IPostBackEventHandler interfaces.

The RegisterAsyncPostBackControl method enables you to register Web server controls as triggers so that they perform an asynchronous postback instead of a synchronous postback. When the ChildrenAsTriggers property of an UpdatePanel control is set to true (which is the default), postback controls inside the UpdatePanel control are automatically registered as asynchronous postback controls.

Use the RegisterAsyncPostBackControl method to register controls outside an UpdatePanel control as triggers for asynchronous postbacks, and to potentially update the content of an update panel. To update an UpdatePanel control programmatically, call the Update method.

You can add the trigger declaratively by using the Triggers element of the UpdatePanel control. In Visual Studio, use the designer's UpdatePanelTrigger Collection Editor dialog box.

The following example shows how to call the RegisterAsyncPostBackControl method to register a Button control so that it causes an update of an UpdatePanel control's content. The Button control is not inside the UpdatePanel control. Two buttons are shown: Button1 and Button2. Button1 refreshes the content of the panel, and Button2 refreshes the whole page.

<%@ 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 EventArgs)
        ScriptManager1.RegisterAsyncPostBackControl(Button1)
    End Sub

    Protected Sub Button1_Click(ByVal Sender As Object, ByVal E As EventArgs)
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString()
    End Sub

    Protected Sub Button2_Click(ByVal Sender As Object, ByVal E As EventArgs)
        Label1.Text = "Page refreshed."
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ScriptManager RegisterAsyncPostBackControl Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"/>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <fieldset>
        <legend>Update Panel</legend>
        <asp:Label ID="Label1" runat="server">Initial postback occurred.</asp:Label>
        </fieldset>
        </ContentTemplate>
        </asp:UpdatePanel>
        <asp:Button ID="Button1" runat="server" Text="Update Panel" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Refresh Page" OnClick="Button2_Click" />
        </div>
    </form>
</body>
</html>

The following example shows how to call the RegisterAsyncPostBackControl method to register a user control so that it causes an update of an UpdatePanel control's content. The first example shows a page using the user control. The second example shows the user control.


<%@ Page Language="VB" %>

<%@ Register Src="Controls/WebUserControl.ascx" TagName="WebUserControl"
	TagPrefix="uc1" %>

<!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 EventArgs)
        ScriptManager1.RegisterAsyncPostBackControl(WebUserControl1)
    End Sub


    Protected Sub Button1_Click(ByVal Sender As Object, ByVal E As EventArgs)
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString()
    End Sub


    Protected Sub WebUserControl1_Click(ByVal Sender As Object, ByVal E As EventArgs)
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString() + _
            ".  Welcome " + WebUserControl1.Name + ". "
    End Sub


</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ScriptManager RegisterAsyncPostBackControl Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"/>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset>
                        <legend>Update Panel</legend>
                        <asp:Label ID="Label1" runat="server">Initial postback occurred.</asp:Label>
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:Button ID="Button1" runat="server" Text="Update Panel" OnClick="Button1_Click" />
            <uc1:webusercontrol id="WebUserControl1" runat="server" oninnerclick="WebUserControl1_Click" />
        </div>
    </form>
</body>
</html>
<%@ Control Language="VB" ClassName="WebUserControl" %>

<script runat="server">
    Public Custom Event InnerClick As EventHandler
        AddHandler(ByVal value As EventHandler)
            AddHandler UCButton1.Click, value
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            RemoveHandler UCButton1.Click, value
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        End RaiseEvent
    End Event

    Public ReadOnly Property Name() As String
        Get
            Return UCTextBox1.Text
        End Get
    End Property
</script>

<asp:Panel ID="UCPanel1" runat="server" GroupingText="User Control">
    Enter your name:
    <asp:TextBox ID="UCTextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="UCButton1" runat="server" Text="Submit" />
</asp:Panel>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5
Show:
© 2015 Microsoft