I have more than one UpdatePanel inside a webform. Inside a UpdatePanel I have a button which triggers AsyncPostBack. According to my requirement I need to cancel any previous pending AsyncPostBack triggered by this button before triggering a new AsyncPostBack, but without aborting any other postback.
For instance when I cancel postback for the update panel upPostback I don't want to cancel the postback for the update panel upPostback2.
Mark Up:
<asp:UpdatePanel ID="upPostback" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:HiddenField ID="hiddenCounter" runat="server" Value="" />
<asp:Button ID="btnDoPostback" runat="server" Text="Click Me" OnClick="OnDoPostBack_Click"
OnClientClick="CancelPreviousPostBack()" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upPostBack2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnDoPostBack2" runat="server" Text="Click Me2" OnClick="OnDoPostBack2_Click" />
</ContentTemplate>
</asp:UpdatePanel>
The function CancelPreviousPostBack checks if there is an AsyncPostBack going on if yes then calls the abortPostBack method of the PageRequestManager to abort any existing AsyncPostBack's and then continue with the new post back.
JavaScript:
function CancelPreviousPostBack() {
var prm = Sys.WebForms.PageRequestManager.getInstance();
var isInPostback = prm.get_isInAsyncPostBack();
if (isInPostback) {
// cancel the previous postback
prm.abortPostBack();
var hiddenCounter = document.getElementById('hiddenCounter');
hiddenCounter.value = counter;
counter++;
}
}
But when the abortPostBack function of the PageRequestManager is called it aborts all the AsyncPostBacks going on in the form.
I couldn't find any method to find out which control triggered the postback if I call the CancelPreviousPostBack function on the OnClientClick Event of the Button. The only way to find out the Id of the control that triggered the postback was to add an event handler to the add_beginRequest method of the PageRequestManager. So I modified my code.
Mark Up:
<asp:Button ID="btnDoPostback" runat="server" Text="Click Me" OnClick="OnDoPostBack_Click" />
JavaScript:
var counter = 0;
var _isInitialLoad = true;
function pageLoad() {
if (_isInitialLoad) {
_isInitialLoad = false;
// hook the events
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(CancelPreviousPostBack);
}
}
function CancelPreviousPostBack(sender, args) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
var isInPostback = prm.get_isInAsyncPostBack();
var postBackElementId = args.get_postBackElement().id;
if (isInPostback && postBackElementId == 'btnDoPostback') {
// cancel the previous postback
prm.abortPostBack();
var hiddenCounter = document.getElementById('hiddenCounter');
hiddenCounter.value = counter;
counter++;
}
}
Now when there are pending postback's prm.get_isInAsyncPostBack() should return true, but strangely it returns false. Even if it returned true it doesn't solve my problem prm.abortPostBack() function aborts all the ongoing postbacks.