2010-07-04 49 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

相关问题