Identifying control that caused postback
I have a page that postbacks on a dropdown list selection (using AJAX update panel). Based 开发者_JAVA技巧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.
}
Check if the dropdownbox has AutoPostBack="true", because that will cause the dropdown to post back even without the submit button click, so if you click the submit button it will postback twice.
One of the ways to find the control which caused postback is to check the Request.Params.Get("__EVENTTARGET");
精彩评论