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.
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);
}
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With