Search code examples
javascriptasp.netajaxonclickonclientclick

ASP.NET: how to make onClientClick finish work before postback?


I have such a control:

<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects"
        OnClientClick="  a= saveAllFilt(); return true; "   onclick="save_all_filt_Click" />

saveAllFilt contains a JQuery ajax call. So what I see in log, I have simultaneous start of postback and saveAllFilt. Postback however "delays" and does nothing before Ajax call in saveAllFilt fninshes.

I wonder if there is a way to do like this: 1. onClientClick, system goes into saveAllFilt JS function and somehow waits(!) till Ajax function finishes in this or that way. 2. Only when it is finished, a real postback starts to happen.

I do realise that JQuery ajax call is event-driven and asynchronous. Just would like to block these postback actions till it finishes (AJAX call prepares data for retrieval that happens via server postback.)


Solution

  • Set the async property of the jQuery AJAX config object to false:

    $.ajax({
        /* ... */
        async : false
        /* ... */
    })