Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying control that caused postback

Tags:

c#

asp.net

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.

like image 618
softwarematter Avatar asked Dec 10 '22 06:12

softwarematter


2 Answers

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);
}
like image 132
Robin Maben Avatar answered Dec 11 '22 19:12

Robin Maben


Use separate server event handlers for your controls. For example:

public void DropDown_Changed(Object sender, EventArgs e)
{
    // Drop down is changed. It's the source of post back.
}

public void Button_Click(Object sender, EventArgs e)
{
    // Button is the source of postback.
}
like image 23
Saeed Neamati Avatar answered Dec 11 '22 19:12

Saeed Neamati