ASP.NET 防止用户控件中的更新面板导致主页面加载执行

ASP.NET Prevent updatepanel in user control from causing main page load to execute

我有一个 ASP.NET 包含更新面板的用户控件。当我在更新面板中触发回发时,它会触发包含用户控件的页面的 page_load 事件,但页面不会像往常一样刷新(不使用更新面板)。 我想知道我是否可以阻止这种情况发生,如果可以,怎么做?

下面是更新面板代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="testControl.ascx.cs" Inherits="WebApplication1.testControl" %>

<asp:ScriptManager runat="server" id="smTest"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="updTest" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnTest" />
    </Triggers>
    <ContentTemplate>
        <asp:Button runat="server" ID="btnTest" Text="test" />
    </ContentTemplate>
</asp:UpdatePanel>

您可以检查 PostBack 是否由页面 and/or UserControl 的 Page_load 中的 UpdatePanel 触发。但是你不能停止Page_Load的执行。

protected void Page_Load(object sender, EventArgs e)
{
    if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
    {
        //updatepanel page load
    }
    else
    {
        //normal page load
    }
}