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;

        }

    }