How To Find Which Control Raised PostBack

There are times in when I really want to know which control in the page raised caused the page to post back. An easy way to find this is to register an event with the control that raises PostBack and cast the sender property to the appropriate control as shown below.



As shown in the above code I am casting the sender property to the appropriate control to find out which control raised the PostBack.  Although finding which control raised the event from the click event handler is nice, but sometimes that is too late in the page life cycle. In a scenario where you have to create controls dynamically, you cannot create controls no later than page load event . In this case, it becomes mandatory for you to know which control raised the PostBack and depending on the situation you may decide to perform certain action. The code shown below shows you how to find which control raised PostBack in page load or earlier events.


The code above is basically broken apart into two conditions. If the PostBack is done from any control that is not rendered as submit button for instance linkbutton, or checkbox control, than will populate __eventtarget property in the request collection to the id of the control that raised the PostBack. However if the PostBack happened because of a submit button, than one of ids in the form collection would represent the id of the button that raised the PostBack.

In the small example above, I illustrated how to get the control that raised the PostBack early in the page life cycle. This will be beneficial mostly when you want to create controls dynamically based on what control caused the PostBack.


Comments have been disabled for this content.