Asp.net

確定哪個 UpdatePanel 導致部分(非同步)回發?

  • January 19, 2018

在一個頁面中包含兩個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總是falseRender階段之前。由於您在該Load階段需要該資訊,因此它不會按預期工作。看到這個錯誤

這裡記錄了一種解決方法,包括派生您自己的類UpdatePanel以訪問其受保護的RequiresUpdate屬性:

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

在您的頁面標記中替換asp:UpdatePanel為後,上面的程式碼變為:ExtendedUpdatePanel

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.
       }
   }
}

引用自:https://stackoverflow.com/questions/4851694