<p>We have some legacy code that needs to identify in the Page_Load which event caused the postback.
At the moment this is implemented by checking the Request data like this...</p>

<p>if (Request.Form["__EVENTTARGET"] != null<br>
&amp;&amp; (Request.Form["__EVENTTARGET"].IndexOf("BaseGrid") > -1 // BaseGrid event ( e.g. sort)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| Request.Form["btnSave"] != null            // Save button </p>

<p>This is pretty ugly and breaks if someone renames a control. Is there a better way of doing this?</p>

<p>Rewriting each page so that it does not need to check this in Page_Load is not an option at the moment.</p>

[{'answer_id': 49311, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 4, 'selected': True, 'text': '<p>This should get you the control that caused the postback:</p>\n\n<pre><code>public static Control GetPostBackControl(Page page)\n{\n    Control control = null;\n\n    string ctrlname = page.Request.Params.Get("__EVENTTARGET");\n    if (ctrlname != null &amp;&amp; ctrlname != string.Empty)\n    {\n        control = page.FindControl(ctrlname);\n    }\n    else\n    {\n        foreach (string ctl in page.Request.Form)\n        {\n            Control c = page.FindControl(ctl);\n            if (c is System.Web.UI.WebControls.Button)\n            {\n                control = c;\n                break;\n            }\n        }\n    }\n    return control;\n}\n</code></pre>\n\n<p>Read more about this on this page:\n<a href="http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx" rel="noreferrer">http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx</a></p>\n'}, {'answer_id': 15296527, 'author': 'AProgrammer', 'author_id': 1684424, 'author_profile': 'https://Stackoverflow.com/users/1684424', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to the above code, if control is of type ImageButton then add the below code,</p>\n\n<pre><code>if (control == null) \n{ for (int i = 0; i &lt; page.Request.Form.Count; i++) \n    { \n        if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y"))) \n             { control = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2)); break; \n             }\n     }\n } \n</code></pre>\n'}, {'answer_id': 15296614, 'author': 'AProgrammer', 'author_id': 1684424, 'author_profile': 'https://Stackoverflow.com/users/1684424', 'pm_score': 0, 'selected': False, 'text': '<p>I am just posting the entire code (which includes the image button / additional control check that causes postback). Thanks Espo.</p>\n\n<pre><code>public Control GetPostBackControl(Page page)\n{ \n   Control control = null; \n   string ctrlname = page.Request.Params.Get("__EVENTTARGET"); \n   if ((ctrlname != null) &amp; ctrlname != string.Empty)\n      { \n         control = page.FindControl(ctrlname); \n       }\n  else \n      {\n        foreach (string ctl in page.Request.Form) \n           { \n              Control c = page.FindControl(ctl); \n              if (c is System.Web.UI.WebControls.Button) \n                  { control = c; break; }\n           }\n       }\n// handle the ImageButton postbacks \nif (control == null) \n{ for (int i = 0; i &lt; page.Request.Form.Count; i++) \n    { \n        if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y"))) \n             { control = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2)); break; \n             }\n     }\n } \nreturn control; \n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/49302', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1127460/']