ScriptManager.RegisterAsyncPostBackControl(Control) 方法

定义

将控件注册为异步回发的触发器。

public:
 virtual void RegisterAsyncPostBackControl(System::Web::UI::Control ^ control);
public void RegisterAsyncPostBackControl (System.Web.UI.Control control);
abstract member RegisterAsyncPostBackControl : System.Web.UI.Control -> unit
override this.RegisterAsyncPostBackControl : System.Web.UI.Control -> unit
Public Sub RegisterAsyncPostBackControl (control As Control)

参数

control
Control

要为异步回发注册的控件。

例外

controlnull

将页面注册为异步回发控件。

  • 或 -

注册的控件不实现 INamingContainerIPostBackDataHandlerIPostBackEventHandler 接口。

示例

下面的示例演示如何调用 RegisterAsyncPostBackControl 方法来注册 Button 控件,以便它导致控件的内容更新 UpdatePanel 。 控件 Button 不在控件内 UpdatePanel 。 显示两个按钮: Button1Button2Button1 刷新面板的内容,并 Button2 刷新整个页面。

<%@ 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)
    {
        ScriptManager1.RegisterAsyncPostBackControl(Button1);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Text = "Page refreshed.";
    }
</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>
<%@ 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>

下面的示例演示如何调用 RegisterAsyncPostBackControl 方法来注册用户控件,以便它导致控件的内容更新 UpdatePanel 。 第一个示例显示使用用户控件的页面。 第二个示例显示用户控件。


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

<%@ 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 void Page_Load(object sender, EventArgs e)
    {
        ScriptManager1.RegisterAsyncPostBackControl(WebUserControl1);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();
    }
    
    protected void WebUserControl1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString() +
            ".  Welcome " + WebUserControl1.Name + ". ";
    }

</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>

<%@ 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>

注解

使用此方法 RegisterAsyncPostBackControl 可将 Web 服务器控件注册为触发器,以便它们执行异步回发而不是同步回发。 当 ChildrenAsTriggers 控件的属性 UpdatePanel 设置为 true (这是默认) 时,控件内的 UpdatePanel 回发控件会自动注册为异步回发控件。

使用此方法将 RegisterAsyncPostBackControl 控件外部 UpdatePanel 的控件注册为异步回发的触发器,并可能更新更新面板的内容。 若要以编程方式更新 UpdatePanel 控件,请调用 Update 该方法。

可以使用控件的UpdatePanel元素以声明方式Triggers添加触发器。 在Visual Studio中,使用设计器的 UpdatePanelTrigger 集合编辑器 对话框。

适用于

另请参阅