Note that there are some explanatory texts on larger screens.

plurals
  1. POGet ASP.NET control which fired a postback within a AJAX UpdatePanel
    text
    copied!<p>Related to this question: <a href="https://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event">On postback, how can I check which control cause postback in Page_Init event</a></p> <p>If the control is wrapped in an ASP.NET AJAX UpdatePanel, the variable "control" is empty because it has a different ID after the AJAX PostBack. Is there a solution to get the control which fired a postback within an ASP.NET Ajax UpdatePanel?</p> <pre><code>public static string GetPostBackControlName( Page page ) { Control control = null; /** * First we will check the "__EVENTTARGET" because if the postback is made * by controls which used the _doPostBack function, it will be available in the Request.Form collection. */ string ctrlname = page.Request.Params["__EVENTTARGET"]; if ( !String.IsNullOrEmpty( ctrlname ) ) { control = page.FindControl( ctrlname ); } else { /** * If __EVENTTARGER is null, the control is a button-type and * need to iterate over the form collection to find it. */ Control c = null; string ctrlStr = null; foreach ( string ctl in page.Request.Form ) { if ( ctl.EndsWith( ".x" ) || ctl.EndsWith( ".y" ) ) { /** * ImageButtons have an additional "quasi-property" in their ID which identifies * the mouse-coordinates (X and Y). */ ctrlStr = ctl.Substring( 0, ctl.Length - 2 ); c = page.FindControl( ctrlStr ); } else { c = page.FindControl( ctl ); } if ( c is Button || c is ImageButton ) { control = c; break; } } } if ( control != null ) { return control.ID; } return string.Empty; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload