加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 确定哪个UpdatePanel导致部分(异步)PostBack?

发布时间:2020-12-15 22:53:57 所属栏目:asp.Net 来源:网络整理
导读:在一个页面中包含两个UpdatePanels,如何知道哪个UpdatePanel导致部分PostBack? 我的意思是在Page_Load事件处理程序中. 这是我的代码: asp:ScriptManager ID="ScriptManager1" runat="server" /asp:ScriptManager asp:UpdatePanel ID="UpdatePanel1" runat=
在一个页面中包含两个UpdatePanels,如何知道哪个UpdatePanel导致部分PostBack?

我的意思是在Page_Load事件处理程序中.

这是我的代码:

<asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" 
     onprerender="UpdatePanel1_PreRender">
     <ContentTemplate>
         <A:u1 ID="u1" runat="server" />
     </ContentTemplate>
 </asp:UpdatePanel>
 <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" 
     onprerender="UpdatePanel2_PreRender">
     <ContentTemplate>
         <A:u2 ID="u2" runat="server" />
     </ContentTemplate>
 </asp:UpdatePanel>

我尝试过这个代码,但是它并没有起作用!

protected void Page_Load(object sender,EventArgs e)
{
    if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
    {
        if (UpdatePanel1.IsInPartialRendering)
        {
            // never enter to here
        }
        if (UpdatePanel2.IsInPartialRendering)
        {
            // neither here
        }
    }
}

任何帮助!

解决方法

您可以使用 UpdatePanel类的 IsInPartialRendering属性来确定特定面板是否导致部分回发:
protected void Page_Render(object sender,EventArgs e)
{
    if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack) {
        if (yourFirstUpdatePanel.IsInPartialRendering) {
            // The first UpdatePanel caused the partial postback.
        } else if (yourSecondUpdatePanel.IsInPartialRendering) {
            // The second UpdatePanel caused the partial postback.
        }
    }
}

编辑:在渲染阶段之前,IsInPartialRendering看起来总是假的.由于您在加载阶段需要该信息,因此无法正常工作.见this bug.

有一个解决方法documented here,其中包括从UpdatePanel派生您自己的类来访问其受保护的RequiresUpdate属性:

public class ExtendedUpdatePanel : UpdatePanel
{
    public bool IsUpdating
    {
        get {
            return RequiresUpdate;
        }
    }
}

在页面标记中使用ExtendedUpdatePanel替换asp:UpdatePanel之后,上面的代码变成:

protected void Page_Load(object sender,EventArgs e)
{
    if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack) {
        if (yourFirstUpdatePanel.IsUpdating) {
            // The first UpdatePanel caused the partial postback.
        } else if (yourSecondUpdatePanel.IsUpdating) {
            // The second UpdatePanel caused the partial postback.
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读