I have a button encoded as such:
<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects"
ClientIDMode="Static" OnClientClick="a= saveAllFilt(); return a; "
OnClick="save_all_filt_Click" />
saveAllFilt is a function that invokes usual JQuery ajax call. Something like this:
function saveAllFilt() {
jQuery.ajax({
async: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
type: "POST",
url: contentURL,
data: dataarray,
success: function (msg) {
// Some processing here. ONLY WHEN IT IS DONE, I NEED TO HAVE POSTBACK.
},
error: function (data) {
}
}); // end of ajax call.
}
I want such a thing: the onClick should proceed only after ajax call is complete. How I can do that?
You can remove the
OnClick
attribute from the button and return false in theOnClientClick
attribute like:and set a hidden button with that
OnClick
attribute like:and in the success method of your
ajax
call click that button like:If return true is not working inside the ajax success function. use __postback fucntion inside the ajax success function.
this will commit postback only after the ajax call complete.
You're not far from getting it right.
Instead of:
<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" ClientIDMode="Static" OnClientClick="a= saveAllFilt(); return a; " OnClick="save_all_filt_Click" />
Try:
<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" ClientIDMode="Static" OnClientClick="return saveAllFilt()" OnClick="save_all_filt_Click" />
The difference is that instead of
OnClientClick="a= saveAllFilt(); return a;"
just doOnClientClick="return saveAllFilt()"
Try to
return true
after your code in your.ajax()
calls'ssuccess
attribute. The priority is always this way :Only if the client returns
true
the server click will initiate.Also, remove the
onclick
attribute - an non-performant attribute in HTML (in my opinion) and use jquery'son()
.it is quite easy. Call your postback method in success function. Chekout the following code.
change your markup
change your java script code
Your idea seems pretty strange to me.Why you are trying to call ajax before postback.
In the function call,
In Ajax,
return true for if the ajax call is success and return false for every other cases.