如何检查的UpdatePanel是否回发?UpdatePanel

2023-09-03 04:55:02 作者:自娱自乐自己过

有没有一种方法,以确定是否< ASP:UpdatePanel中/> 执行了阿贾克斯回发类似我们如何使用...

 如果(!Page.IsPostBack){...}剪断
 

...,以确定是否从一个按钮回发提交正在发生。

我想检测从jQuery的Ajax请求,但它拿起UpdatePanel的要求,以及我要排除如...

 如果(Request.IsAjaxRequest()及和放大器;!Page.IsUpdatePanelPostback)
{
    //处理jQuery的阿贾克斯
}
 

解决方案

我不知道这是否会工作,任何比你更好的解决方案,但你尝试过:

 如果(ScriptManager.GetCurrent(页).IsInAsyncPostBack)
{
    控制CTRL = GetControlThatCausedPostBack(页);
    如果(CTRL为的UpdatePanel)
    {
        //处理的UpdatePanel回发
    }
}

私人控制GetControlThatCausedPostBack(页)
{
    //初始化控制,并将其设置为null
    控制CTRL = NULL;

    //获取事件目标名称,并找到控制
    字符串ctrlName = Page.Request.Params.Get(__ EVENTTARGET);
    如果(!String.IsNullOrEmpty(ctrlName))
        CTRL = page.FindControl(ctrlName);

    //控制返回给调用方法
    返回CTRL;
}
 
怎样解决Windows Update当前无法检查更新的问题

Is there a way to determine if an <asp:UpdatePanel /> has performed an Ajax postback similar to how we can use...

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

... to determine if a postback from a button submit is taking place.

I'm trying to detect Ajax requests from jQuery, but it's picking up UpdatePanel requests as well which I want to exclude eg...

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

解决方案

I don't know if this will work any better than your solution, but have you tried?:

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