Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net OnClientClick has an async call - how to proceed with server-side OnClick only when that async call is complete and successful?

I have

<asp:Button ID="Submit_BT" runat="server" OnClientClick="return clientClick();" OnClick="Submit_BT_Click" />

And then in JS:

function clientClick(){
     // using jQuery
     $.get(..., function(result){
         // Perform some processing on result
         // Proceed with the server-side postback
     },
     function(error){
         // Don't proceed with server-side postback
     });

     return true; // ?---
}

I want the $.get to successfully complete before postback, but I think postback will never happen, or will always happen. Can I simply say return $.get(...); How to do this?

This question is similar, and the solution is to set async: false, which partially answers my question. When the call completes, it could be either successful or erroneous. Only when the call is successful do I want to proceed with the server-side click. How do I "return true or false" from inside the $.get()'s handlers?

like image 985
Mickael Caruso Avatar asked Oct 26 '25 19:10

Mickael Caruso


1 Answers

You can always return false in your JS function to assure that it won't postback on click.

And do manual postback in succesful async callback:

$.get(..., function(result){

   // Perform some processing on result

   __doPostBack('Submit_BT', '')

}
like image 81
Yuriy Galanter Avatar answered Oct 28 '25 09:10

Yuriy Galanter