【问题标题】:How to check whether UpdatePanel is posting back?如何检查 UpdatePanel 是否回发?
【发布时间】:2011-11-18 14:54:34
【问题描述】:

有没有办法确定<asp:UpdatePanel /> 是否执行了类似于我们可以使用的 Ajax 回发...

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

...确定按钮提交的回发是否正在发生。

我正在尝试检测来自 jQuery 的 Ajax 请求,但它也在接收我想排除的 UpdatePanel 请求,例如...

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

【问题讨论】:

    标签: asp.net .net ajax updatepanel http-post


    【解决方案1】:

    您可以检查回发是否是异步的,以及它是否由查看这些属性的更新面板发出:

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

    【讨论】:

      【解决方案2】:

      我不知道这是否会比您的解决方案更好,但是您尝试过吗?:

      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',因为我猜来自 ASP.NET AJAX Extensions 的其他控件可能会触发我不会触发的 Ajax 请求想要取货。
      • 我想值得一试。
      【解决方案3】:

      试试以下:

      var controlName = Page.Request.Params.Get("__EVENTTARGET");
      if (!String.IsNullOrEmpty(controlName))
      {
           // Use FindControl(controlName) to see whether 
           // control is of UpdatePanel type
      }
      

      有用的链接:

      【讨论】:

      • 这可能有效,但它只检测特定的 UpdatePanel 不是吗?我正在寻找一个更“通用”的解决方案来检测任何 UpdatePanel。
      • 没问题,您可以使用 FindControl() 来查看此控件是否属于 UpdatePanel 类型...我已经更新了答案
      猜你喜欢
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多