Click here to Skip to main content
15,885,537 members
Articles / Web Development / ASP.NET
Tip/Trick

Find which updatepanel caused the post?

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
27 Jan 2010CPOL 17.5K   6   1
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)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionwhat if it's not inside the updatepanel and it's a trigger to it ? Pin
saharmizrahi@yahoo.com16-Jul-12 21:56
saharmizrahi@yahoo.com16-Jul-12 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.