Asp.net

如何檢查 UpdatePanel 是否正在回發?

  • April 20, 2015

有沒有辦法確定是否<asp:UpdatePanel />執行了類似於我們可以使用的 Ajax 回發…

if(!Page.IsPostBack) { ...snip }

…確定是否正在發生來自按鈕送出的回發。

我正在嘗試檢測來自 jQuery 的 Ajax 請求,但它也在接收我想排除的 UpdatePanel 請求,例如……

if (Request.IsAjaxRequest() && !Page.IsUpdatePanelPostback)
{
   // Deal with jQuery Ajax
}

我不知道這是否會比您的解決方案更好,但是您嘗試過嗎?:

if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
{
   Control ctrl = GetControlThatCausedPostBack(Page);
   if (ctrl is UpdatePanel)
   {
       //handle updatepanel postback
   }
}

private Control GetControlThatCausedPostBack(Page page)
{
   //initialize a control and set it to null
   Control ctrl = null;

   //get the event target name and find the control
   string ctrlName = Page.Request.Params.Get("__EVENTTARGET");
   if (!String.IsNullOrEmpty(ctrlName))
       ctrl = page.FindControl(ctrlName);

   //return the control to the calling method
   return ctrl;
}

您可以檢查回發是否是非同步的,以及它是否由查看這些屬性的更新面板發出:

ScriptManager.GetCurrent(Page).IsInAsyncPostback
ScriptManager.GetCurrent(Page).AsyncPostbackSourceElementID

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