2011-11-18 63 views
8

有沒有一種方法,以確定是否<asp:UpdatePanel />已經進行類似於我們如何能夠利用一個Ajax回發...如何檢查UpdatePanel是否回發?

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

...,以確定是否從一個按鈕回傳提交正在發生。

我試圖檢測來自jQuery的Ajax請求,但它撿的UpdatePanel請求以及其中我要排除如...

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

回答

12

我不知道這是否會工作,任何比你更好的解決方案,但你嘗試過?:

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

我想知道如果我可以使用'ScriptManager.GetCurrent(Page).IsInAsyncPostBack'自己的,因爲我猜想ASP.NET AJAX Extensions的其他控件可能會觸發我不想拾取的Ajax請求。 –

+0

這是值得的,我猜想。 –

3

試試以下:

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

有用的鏈接:

+0

這可能會工作,但它只檢測特定的UpdatePanel不是嗎?我在檢測任何UpdatePanel的更通用的解決方案之後。 –

+0

沒問題,你可以使用FindControl()來查看這個控件是否是UpdatePanel類型的......我更新了我的答案 – sll

9

您可以檢查回發是否同步,是否被更新面板看發行在這些屬性:

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