Click here to Skip to main content
Click here to Skip to main content

Find which updatepanel caused the post?

, 27 Jan 2010
Rate this:
Please Sign up or sign in to vote.
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:

ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID

 
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)

About the Author

arash2009

Australia Australia
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 28 Jan 2010
Article Copyright 2010 by arash2009
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid