Is there a way to determine if an <asp:UpdatePanel /> has performed an Ajax postback similar to how we can use…
if(!Page.IsPostBack) { ...snip }
… to determine if a postback from a button submit is taking place.
I’m trying to detect Ajax requests from jQuery, but it’s picking up UpdatePanel requests as well which I want to exclude eg…
if (Request.IsAjaxRequest() && !Page.IsUpdatePanelPostback)
{
// Deal with jQuery Ajax
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can check whether the postback was asynchronous and whether it was issued by an update panel looking at these properties:
ScriptManager.GetCurrent(Page).IsInAsyncPostback ScriptManager.GetCurrent(Page).AsyncPostbackSourceElementID
Method 2
I don’t know if this will work any better than your solution, but have you tried?:
if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
{
Control ctrl = GetControlThatCausedPostBack(Page);
if (ctrl is UpdatePanel)
{
//handle updatepanel postback
}
}
private Control GetControlThatCausedPostBack(Page page)
{
//initialize a control and set it to null
Control ctrl = null;
//get the event target name and find the control
string ctrlName = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
ctrl = page.FindControl(ctrlName);
//return the control to the calling method
return ctrl;
}
Method 3
Try out following:
var controlName = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(controlName))
{
// Use FindControl(controlName) to see whether
// control is of UpdatePanel type
}
Helpful links:
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0