如何检查UpdatePanel是否回发?

Sun*_*oot 10 .net asp.net ajax updatepanel http-post

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

if(!Page.IsPostBack) { ...snip }
Run Code Online (Sandbox Code Playgroud)

...确定是否正在进行按钮提交的回发.

我正在尝试从jQuery中检测Ajax请求,但它正在拾取UpdatePanel请求,我想要排除,例如......

if (Request.IsAjaxRequest() && !Page.IsUpdatePanelPostback)
{
    // Deal with jQuery Ajax
}
Run Code Online (Sandbox Code Playgroud)

kay*_*zam 16

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

ScriptManager.GetCurrent(Page).IsInAsyncPostback
ScriptManager.GetCurrent(Page).AsyncPostbackSourceElementID
Run Code Online (Sandbox Code Playgroud)


Jam*_*son 14

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

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;
}
Run Code Online (Sandbox Code Playgroud)