AsyncPostBackTrigger 类

定义

定义一个控件,并将该控件的可选事件定义为导致 UpdatePanel 控件刷新的异步回发控件触发器。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
继承

示例

下面的示例演示如何以声明方式AsyncPostBackTrigger添加控件。The following example shows how to add an AsyncPostBackTrigger control declaratively. 控件之外的Products控件允许用户输入搜索词, 以便在 Northwind 数据库的表中查找。 Button UpdatePanelA Button control that is outside an UpdatePanel control lets users enter a search term to find in the Products table of the Northwind database. 控件内的GridView UpdatePanel控件显示结果。A GridView control that is inside the UpdatePanel control shows the results. 控件被指定为UpdatePanel控件的异步触发器。 ButtonThe 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>

注解

使用控件使控件成为UpdatePanel控件的触发器。 AsyncPostBackTriggerUse 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.

  • ChildrenAsTriggers属性为false时, 位于面板内的控件。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.

AsyncPostBackTrigger控件引用的控件必须与作为其触发器的更新面板位于同一命名容器中。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.

通过AsyncPostBackTrigger使用设计器中的 " UpdatePanelTrigger 集合编辑器" 对话框Triggers或通过在UpdatePanel控件中以声明方式创建元素来添加控件。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. 属性是必需的, 但属性是可选的EventNameControlIDThe ControlID property is required, but the EventName property is optional. 如果未指定DefaultEventAttribute 属性,则使用控件的属性来EventName确定默认事件。If the EventName property is not specified, the DefaultEventAttribute attribute of the control is used to determine the default event. 例如, Button控件的默认事件Click为事件。For example, the default event for the Button control is the Click event. EventName属性不区分大小写。The EventName property is case-insensitive.

还可以将命名容器引用为触发器。You can also reference a naming container as a trigger. 在这种情况下, 会将导致回发的容器中的所有子控件视为UpdatePanel控件的触发器。In that case, all child controls in the container that cause a postback are considered triggers for the UpdatePanel control.

不支持AsyncPostBackTrigger以编程方式添加控件。Programmatically adding AsyncPostBackTrigger controls is not supported. 若要以编程方式注册回发控件RegisterAsyncPostBackControl , 请使用ScriptManager控件的方法。To programmatically register a postback control, use the RegisterAsyncPostBackControl method of the ScriptManager control. 然后, 在Update控件回发UpdatePanel时调用控件的方法。Then call the Update method of the UpdatePanel control when the control posts back.

如果使用PostBackTriggerAsyncPostBackTrigger来定义控件, 则会引发异常。If you define a control using both PostBackTrigger and AsyncPostBackTrigger, an exception is thrown.

构造函数

AsyncPostBackTrigger()

初始化 AsyncPostBackTrigger 类的新实例。Initializes a new instance of an AsyncPostBackTrigger class.

属性

ControlID

获取或设置控件的名称,该控件会触发 UpdatePanel 控件的异步回发。Gets or sets the name of the control that triggers an asynchronous postback for an UpdatePanel control.

EventName

获取或设置触发对 UpdatePanel 控件进行更新的回发控件事件。Gets or sets the postback control event that triggers an UpdatePanel control to be updated.

Owner

获取对 UpdatePanel 所面向的 UpdatePanelTrigger 控件的引用。Gets a reference to the UpdatePanel control that the UpdatePanelTrigger targets.

(继承自 UpdatePanelTrigger)

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindTargetControl(Boolean)

搜索在 ControlID 属性中指定的控件。Searches for the control specified in the ControlID property.

(继承自 UpdatePanelControlTrigger)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
HasTriggered()

返回一个指示是否已调用触发器的值。Returns a value that indicates whether the trigger was invoked.

Initialize()

初始化 AsyncPostBackTrigger 控件,并确定触发器中命名的事件是否存在和有效。Initializes the AsyncPostBackTrigger control and determines whether the event that is named in the trigger exists and is valid.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnEvent(Object, EventArgs)

引发事件,该事件将触发 UpdatePanel 控件的回发和刷新。Raises the event that will trigger a postback and refresh of an UpdatePanel control.

ToString()

返回当前 AsyncPostBackTrigger 控件的字符串表示形式。Returns a string representation of the current AsyncPostBackTrigger control.

适用于

另请参阅