2010-07-04 15 views

回答

104

我看到已經有一些很棒的建議和方法建議如何獲得回發控制。然而,我發現另一個網頁(Mahesh blog)與檢索回發控制ID的方法。

我會在這裏稍微修改一下,包括使其成爲擴展類。希望這種方式更有用。

/// <summary> 
/// Gets the ID of the post back control. 
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx 
/// </summary> 
/// <param name = "page">The page.</param> 
/// <returns></returns> 
public static string GetPostBackControlId(this Page page) 
{ 
    if (!page.IsPostBack) 
     return string.Empty; 

    Control control = null; 
    // first we will check the "__EVENTTARGET" because if post back made by the controls 
    // which used "_doPostBack" function also available in Request.Form collection. 
    string controlName = page.Request.Params["__EVENTTARGET"]; 
    if (!String.IsNullOrEmpty(controlName)) 
    { 
     control = page.FindControl(controlName); 
    } 
    else 
    { 
     // if __EVENTTARGET is null, the control is a button type and we need to 
     // iterate over the form collection to find it 

     // ReSharper disable TooWideLocalVariableScope 
     string controlId; 
     Control foundControl; 
     // ReSharper restore TooWideLocalVariableScope 

     foreach (string ctl in page.Request.Form) 
     { 
      // handle ImageButton they having an additional "quasi-property" 
      // in their Id which identifies mouse x and y coordinates 
      if (ctl.EndsWith(".x") || ctl.EndsWith(".y")) 
      { 
       controlId = ctl.Substring(0, ctl.Length - 2); 
       foundControl = page.FindControl(controlId); 
      } 
      else 
      { 
       foundControl = page.FindControl(ctl); 
      } 

      if (!(foundControl is IButtonControl)) continue; 

      control = foundControl; 
      break; 
     } 
    } 

    return control == null ? String.Empty : control.ID; 
} 

更新(2016年7月22日):爲ButtonImageButton類型檢查改爲尋找IButtonControl允許來自第三方控件回傳被認可。

+3

感謝您的支持......完美的重複使用代碼段。 – trgraglia 2011-12-22 08:22:59

+1

我有一個先進的方案,它不工作 - http://stackoverflow.com/questions/14486733/how-to-check-whether-postback-caused-by-a-dynamic-link-button – Lijo 2013-01-23 18:47:29

+4

不是這個嗎?如果有多個按鈕或圖像按鈕會失敗?看起來它只會返回找到的第一個。 – 2014-05-02 21:03:46

9

直接在表單參數或

string controlName = this.Request.Params.Get("__EVENTTARGET"); 

編輯:要檢查控制造成回發(手動):

// input Image with name="imageName" 
if (this.Request["imageName"+".x"] != null) ...;//caused postBack 

// Other input with name="name" 
if (this.Request["name"] != null) ...;//caused postBack 

你可以也遍歷所有的控件,並檢查是否有一個m使用上面的代碼導致了postBack。

+1

我已經檢查,其不爲我工作。 – 2010-07-04 17:35:39

+0

那麼,你必須知道所有可能導致回發的控件,並檢查參數集合('Request [controlName]')中是否有值。 – 2010-07-04 18:11:21

+0

@JaroslavJandek謝謝你的回答。我有一個需要在頁面OnLoad事件中檢測按鈕點擊的實例,但是由於某種原因,Request [「__ EVENTTARGET」]是空的(即使它不是與其他控件導致回發)。 UniqueID]完美無缺+1 :) – 2012-07-13 14:01:49

3

假設它是一個服務器控件,您可以使用Request["ButtonName"]

要查看是否被點擊一個特定的按鈕:if (Request["ButtonName"] != null)

+3

所有異步回發請求[name]爲空 – 2013-01-18 09:50:57

14

下面是一些代碼,可能會爲你做的伎倆(從Ryan Farley's blog拍攝)

public static Control GetPostBackControl(Page page) 
{ 
    Control control = null; 

    string ctrlname = page.Request.Params.Get("__EVENTTARGET"); 
    if (ctrlname != null && ctrlname != string.Empty) 
    { 
     control = page.FindControl(ctrlname); 
    } 
    else 
    { 
     foreach (string ctl in page.Request.Form) 
     { 
      Control c = page.FindControl(ctl); 
      if (c is System.Web.UI.WebControls.Button) 
      { 
       control = c; 
       break; 
      } 
     } 
    } 
    return control; 
} 
+0

我批准此代碼,我已經使用了一段時間了。 – Alex 2016-03-28 17:23:34

+0

這是正確的代碼,我的答案指的是PageUtility類,我完全忘了它是我用上面的方法創建的類。 – Alex 2016-08-02 20:04:22

8

如果您需要檢查其控制造成回傳,那麼你可以只直接比較["__EVENTTARGET"]來控制你有興趣:

if (specialControl.UniqueID == Page.Request.Params["__EVENTTARGET"]) 
{ 
    /*do special stuff*/ 
} 

這是假設你只是被從比較的結果無論如何,任何GetPostBackControl(...)擴展方法。它可能無法處理所有情況,但如果它起作用,則更簡單。另外,你不會在頁面上尋找你不關心的控件。

+0

這是一個很好和簡單的伎倆。幫助我的情況,當時只是想處理一些非常具體的控制回發。 – Marcel 2018-01-25 14:32:52

3
if (Request.Params["__EVENTTARGET"] != null) 
{ 
    if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID")) 
    { 
    DoWhateverYouWant(); 
    } 
} 
1

的除了以前的答案,使用Request.Params["__EVENTTARGET"]你要設置的選項:

buttonName.UseSubmitBehavior = false; 
1

爲了得到控制的確切名稱,用途:

string controlName = Page.FindControl(Page.Request.Params["__EVENTTARGET"]).ID; 
+1

此代碼不起作用.... – 2015-04-08 10:07:50

相關問題