Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Find which updatepanel caused the post?

4.67/5 (4 votes)
27 Jan 2010CPOL 17.7K  
Have you ever developed a web application with several Ajax Update panels on one page and then wondering which one caused the postback ?Well, this happened to me and after many dead ends, here is the key to finding what UpdatePanel caused the...
Have you ever developed a web application with several Ajax Update panels on one page and then wondering which one caused the postback ?
Well, this happened to me and after many dead ends, here is the key to finding what UpdatePanel caused the Post:
<br />
ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID<br />


This returns the actual element ID that forced the Post. How does this help? Well, any post that originated in a UpdatePanel would be from a child control. So, if you walk up the control tree and find a UpdatePanel, you will have the UpdatePanel that caused the Post.

private UpdatePanel GetUpdatePanelThatCausedPostback()
{
    var c = Page.FindControl(ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID);
    while (c != null && !(c is UpdatePanel)) { c = c.Parent; }
    if (c != null) return (c as UpdatePanel);
    else return null;
}


Here we get the control that actually fired the Post and then we walk up the control tree, one by one, until we are either past the top (null) or we have found an UpdatePanel.
Then we simply either return the newly found UpdatePaenl or we return null.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)