回发时,如何检查哪个控件会导致Page_Init事件回发

回发时,如何检查哪个控件会导致Page_Init事件回发。

protected void Page_Init(object sender, EventArgs e) { //need to check here which control cause postback? } 

谢谢

我看到,已经有一些很好的build议和方法build议如何获得后控制。 但是,我发现另一个网页( Mahesh博客 )与检索回发控制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-07-22):键入检查ButtonImageButton更改为查找IButtonControl以允许识别来自第三方控件的回发。

这里有一些代码可以帮助你(来自Ryan Farley的博客 )

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

直接在表单参数或

 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 

你也可以遍历所有的控件,并检查是否有一个使用上面的代码导致回发。

如果您需要检查哪个控件导致了回发,那么您可以直接将["__EVENTTARGET"]与您感兴趣的控件进行比较:

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

这假设你只是要比较任何GetPostBackControl(...)扩展方法的结果。 它可能无法处理每个情况,但如果它起作用,则更简单。 另外,你不会在页面上寻找一个你不关心的控件。

假设它是一个服务器控件,可以使用Request["ButtonName"]

要查看是否点击了特定的button: if (Request["ButtonName"] != null)

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

除了以前的答案,要使用Request.Params["__EVENTTARGET"]你必须设置选项:

 buttonName.UseSubmitBehavior = false; 

要得到确切的控制名称,请使用:

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