Welcome to MSDN Blogs Sign in | Join | Help

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;

        }

    }

 

 

Published Tuesday, April 10, 2007 4:52 AM by irenak

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: SYSK 325: How To Tell Which UpdatePanel Issued a Page PostBack

Thursday, April 26, 2007 11:16 AM by Dennis

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? Thanks

# re: Reply to Dennis

Thursday, April 26, 2007 12:37 PM by irenak

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.

# re: SYSK 325: How To Tell Which UpdatePanel Issued a Page PostBack

Thursday, April 26, 2007 1:20 PM by cxspan

This doesn't appear to work.  

string updatePanel = parameters[0]; --> this is giving me the id of the scriptmanager, not the updatepanel.

# re: SYSK 325: How To Tell Which UpdatePanel Issued a Page PostBack

Thursday, April 26, 2007 5:11 PM by cxspan

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];

       }

# re: SYSK 325: How To Tell Which UpdatePanel Issued a Page PostBack

Monday, August 06, 2007 9:08 PM by Alexander

Actually you don't need to replace ClientID... just use UniqueID

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker