ASP.NET: how to make onClientClick finish work bef

2019-07-17 20:24发布

问题:

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.)

回答1:

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

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