C# – Identifying control that caused postback

asp.netc

I have a page that postbacks on a dropdown list selection (using AJAX update panel). Based on the dropdown selection, the rest of the UI on the page is generated dynamically. The dynamic UI is drawn on page load for fetching values on Submit button click. The problem I am facing is that on dropdown change, two postbacks seem to happen, one which draws the original UI and one that draws the changed UI (thus creating inconsistency). How to deal with this. Is there any way to figure out which control caused the postback, so that I can redraw UI when postback happens due to selection change/submit button click.

EDIT: Missed an important point in question. The trigger for update panel is SelectionChanged event of dropdown list. That causes the additional postback.

Best Answer

You can check for a postback and then do..

if (IsPostBack)
{ 
  var targetID = Request.Form["__EVENTTARGET"];
}

EDIT: You can get the actual control by doing..

if (targetID != null && targetID != string.Empty)
{
    var targetControl = this.Page.FindControl(targetID);
}
Related Topic