SYSK 325: How To Tell Which UpdatePanel Issued a Page PostBack
If you have multiple UpdatePanel controls on a page, and your Page_Load has a lot of data retrieval and rendering logic, you may want to know exactly which UpdatePanel is being updated, and execute only relevant code segments to optimize performance.
Here is a code snippet that should give you the UpdatePanel control id and the trigger control id that caused the postback.
Important: I’m relying on AJAX code (see below) that’s loaded at runtime as a resource (.axd) and it changes, this code might break.
if (Page.IsPostBack)
{
// Is it an AJAX postback?
foreach (string key in Request.Form.AllKeys)
{
if (key == ScriptManager.GetCurrent(this).ID)
{
string[] parameters = Request.Form[key].Split('|');
string updatePanel = parameters[0];
string ctrl = parameters[1];
break;
}
}
}
Here is the AJAX code I was referring to:
function Sys$WebForms$PageRequestManager$_getPostBackSettings(element, elementUniqueID) {
var originalElement = element;
var proposedSettings = null;
while (element) {
if (element.id) {
if (!proposedSettings && Array.contains(this._asyncPostBackControlClientIDs, element.id)) {
proposedSettings = this._createPostBackSettings(true, this._scriptManagerID + '|' + elementUniqueID, originalElement);
}
else {
if (!proposedSettings && Array.contains(this._postBackControlClientIDs, element.id)) {
return this._createPostBackSettings(false, null, null);
}
else {
var indexOfPanel = Array.indexOf(this._updatePanelClientIDs, element.id);
if (indexOfPanel !== -1) {
if (this._updatePanelHasChildrenAsTriggers[indexOfPanel]) {
return this._createPostBackSettings(true, this._updatePanelIDs[indexOfPanel] + '|' + elementUniqueID, originalElement);
}
else {
return this._createPostBackSettings(true, this._scriptManagerID + '|' + elementUniqueID, originalElement);
}
}
}
}
if (!proposedSettings && this._matchesParentIDInList(element.id, this._asyncPostBackControlClientIDs)) {
proposedSettings = this._createPostBackSettings(true, this._scriptManagerID + '|' + elementUniqueID, originalElement);
}
else {
if (!proposedSettings && this._matchesParentIDInList(element.id, this._postBackControlClientIDs)) {
return this._createPostBackSettings(false, null, null);
}
}
}
element = element.parentNode;
}
if (!proposedSettings) {
return this._createPostBackSettings(false, null, null);
}
else {
return proposedSettings;
}
}
Comments
Anonymous
April 26, 2007
Hi i think that your code i very interesting. I only don't see the connection between your code-behind code and where you invoke the PageRequestManagers method getPostBackSettings. i your code behind you get the scriptmanager and the control which made the postback and whats next? ThanksAnonymous
April 26, 2007
I'm not providing any client-side code that triggers a postback. The AJAX code you see is from Microsoft AJAX library which is listed to explain why the code-behind works.Anonymous
April 26, 2007
This doesn't appear to work. string updatePanel = parameters[0]; --> this is giving me the id of the scriptmanager, not the updatepanel.Anonymous
April 26, 2007
Okay, so it DOES work, but only in specific situations. For example, it doesn't work with masterpages when the ScriptManager is on the masterpage because you are not comparing against the ClientID. Also, the "key" id will be $-delimited while the ScriptManager id will be -delimited. Something like this should work though: if (key == ScriptManager.GetCurrent(this).ClientID.Replace('', '$')) Also, parameters[0] will return the ScriptManager ID if the control that caused the postback is outside of the UpdatePanel, even if it is set as an AsyncPostBackTrigger on that UpdatePanel. Still, this could prove to be very useful. Another way to write it: ScriptManager sm = ScriptManager.GetCurrent(this); if (sm != null && sm.IsInAsyncPostBack) { string[] parameters = Request.Form[sm.ClientID.Replace('_', '$')].Split('|'); string updatePanel = parameters[0]; string ctrl = parameters[1]; }Anonymous
August 06, 2007
Actually you don't need to replace ClientID... just use UniqueID