In the case of postback, how can I check which control causes the postback in the Page_Init event

In the reverse order, how can I check which control causes the postback in the Page_Init event.

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

thank

+52
c # page-lifecycle
Jul 04 '10 at 17:14
source share
8 answers

I see that there are already some great tips and techniques suggesting how to get post control. However, I found another web page ( Mahesh's blog ) using the method of getting the feedback identifier after feedback.

I posted it here with a little modification, including creating an extension class. Hope this is more helpful.

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

Refresh (2016-07-22): Change type for Button and ImageButton changed to search for IButtonControl so that it can recognize postbacks from third-party controls.

+101
Aug 18 '10 at 7:14
source share

Here is the code that can do the trick for you (taken from 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; } 
+14
Jul 04 '10 at 17:25
source share

Either directly in the form parameters, or

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

Change To check if an item called a callback (manually):

 // 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 

You can also iterate over all the controls and check if one of them called postBack the above code.

+9
Jul 04 '10 at 17:20
source share

If you need to check which control caused the ["__EVENTTARGET"] , you can simply compare ["__EVENTTARGET"] with the control that interests you:

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

It is assumed that you will still compare the result with any GetPostBackControl(...) extension method. It may not handle EVERY situation, but if it works, it is easier. In addition, you will not comb through the page that is looking for a control that you did not want to start about.

+7
Jun 28 '13 at 21:27
source share

Assuming this is a server control, you can use Request["ButtonName"]

To find out if a particular button has been pressed: if (Request["ButtonName"] != null)

+3
Jul 04 '10 at 17:20
source share
 if (Request.Params["__EVENTTARGET"] != null) { if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID")) { DoWhateverYouWant(); } } 
+3
Apr 15 '14 at 2:52
source share

In addition to the previous answers, to use Request.Params["__EVENTTARGET"] , you must set this parameter:

 buttonName.UseSubmitBehavior = false; 
+1
Jul 15 '14 at 15:27
source share

To get the exact name of the control, use:

  string controlName = Page.FindControl(Page.Request.Params["__EVENTTARGET"]).ID; 
+1
Dec 18 '14 at 16:18
source share



All Articles