AsyncPostBackTrigger Class

Definition

Defines a control and optional event of the control as an asynchronous postback control trigger that causes an UpdatePanel control to refresh.

public ref class AsyncPostBackTrigger : System::Web::UI::UpdatePanelControlTrigger
public class AsyncPostBackTrigger : System.Web.UI.UpdatePanelControlTrigger
type AsyncPostBackTrigger = class
    inherit UpdatePanelControlTrigger
Public Class AsyncPostBackTrigger
Inherits UpdatePanelControlTrigger
Inheritance

Examples

The following example shows how to add an AsyncPostBackTrigger control declaratively. A Button control that is outside an UpdatePanel control lets users enter a search term to find in the Products table of the Northwind database. A GridView control that is inside the UpdatePanel control shows the results. The Button control is specified as an asynchronous trigger for the 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 Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectParameters["SearchTerm"].DefaultValue = 
            Server.HtmlEncode(TextBox1.Text);
        Label1.Text = "Searching for '" + 
            Server.HtmlEncode(TextBox1.Text) + "'";
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>AsyncPostBackTrigger Example</title>
</head>
<body>
    <form id="form1" defaultbutton="Button1"
          defaultfocus="TextBox1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" Text="Submit" 
                        OnClick="Button1_Click" runat="server"  />
            <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" 
                             runat="server">
                <Triggers>
                  <asp:AsyncPostBackTrigger ControlID="Button1" />
                </Triggers>
                <ContentTemplate>
                    <hr />
                    <asp:Label ID="Label1" runat="server"/>
                    <br />
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                        AllowSorting="True"
                        DataSourceID="SqlDataSource1">
                        <EmptyDataTemplate>
                        Enter a search term.
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                        SelectCommand="SELECT [ProductName], [UnitsInStock] FROM 
                        [Alphabetical list of products] WHERE ([ProductName] LIKE 
                        '%' + @SearchTerm + '%')">
                        <SelectParameters>
                            <asp:Parameter Name="SearchTerm" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </ContentTemplate>
            </asp:UpdatePanel>
        </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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        SqlDataSource1.SelectParameters("SearchTerm").DefaultValue = _
            Server.HtmlEncode(TextBox1.Text)
        Label1.Text = "Searching for '" & _
            Server.HtmlEncode(TextBox1.Text) & "'"

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>AsyncPostBackTrigger Example</title>
</head>
<body>
    <form id="form1" defaultbutton="Button1"
          defaultfocus="TextBox1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" Text="Submit" 
                        OnClick="Button1_Click" runat="server"  />
            <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" 
                             runat="server">
                <Triggers>
                  <asp:AsyncPostBackTrigger ControlID="Button1" />
                </Triggers>
                <ContentTemplate>
                    <hr />
                    <asp:Label ID="Label1" runat="server"/>
                    <br />
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                        AllowSorting="True"
                        DataSourceID="SqlDataSource1">
                        <EmptyDataTemplate>
                        Enter a search term.
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                        SelectCommand="SELECT [ProductName], [UnitsInStock] FROM 
                        [Alphabetical list of products] WHERE ([ProductName] LIKE 
                        '%' + @SearchTerm + '%')">
                        <SelectParameters>
                            <asp:Parameter Name="SearchTerm" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

Remarks

Use the AsyncPostBackTrigger control to enable controls to be triggers for an UpdatePanel control. Controls that are triggers for an update panel cause a refresh of the panel's content after an asynchronous postback. Defining an asynchronous trigger control is useful in the following scenarios:

  • For controls that are outside a panel.

  • For controls that are inside a panel when the ChildrenAsTriggers property is false.

  • For controls that are inside nested panels, in order to cause a refresh of parent panels.

The control that the AsyncPostBackTrigger control references must be in the same naming container as the update panel for which it is a trigger. Triggers that are based on controls in other naming containers are not supported.

Add AsyncPostBackTrigger controls either by using the UpdatePanelTrigger Collection Editor dialog box in the designer or by creating a Triggers element declaratively in the UpdatePanel control. The ControlID property is required, but the EventName property is optional. If the EventName property is not specified, the DefaultEventAttribute attribute of the control is used to determine the default event. For example, the default event for the Button control is the Click event. The EventName property is case-insensitive.

You can also reference a naming container as a trigger. In that case, all child controls in the container that cause a postback are considered triggers for the UpdatePanel control.

Programmatically adding AsyncPostBackTrigger controls is not supported. To programmatically register a postback control, use the RegisterAsyncPostBackControl method of the ScriptManager control. Then call the Update method of the UpdatePanel control when the control posts back.

If you define a control using both PostBackTrigger and AsyncPostBackTrigger, an exception is thrown.

Constructors

AsyncPostBackTrigger()

Initializes a new instance of an AsyncPostBackTrigger class.

Properties

ControlID

Gets or sets the name of the control that triggers an asynchronous postback for an UpdatePanel control.

EventName

Gets or sets the postback control event that triggers an UpdatePanel control to be updated.

Owner

Gets a reference to the UpdatePanel control that the UpdatePanelTrigger targets.

(Inherited from UpdatePanelTrigger)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindTargetControl(Boolean)

Searches for the control specified in the ControlID property.

(Inherited from UpdatePanelControlTrigger)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
HasTriggered()

Returns a value that indicates whether the trigger was invoked.

Initialize()

Initializes the AsyncPostBackTrigger control and determines whether the event that is named in the trigger exists and is valid.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnEvent(Object, EventArgs)

Raises the event that will trigger a postback and refresh of an UpdatePanel control.

ToString()

Returns a string representation of the current AsyncPostBackTrigger control.

Applies to

See also