Block asp: enable OnClick event in C # from OnClientClick event in JavaScript?

I have an asp:Button on my web page and it calls a JavaScript function and a code method. The latter makes a call to go to another page. In the JavaScript function, I check the condition. If this condition is not met, I want to abort the navigation so that the OnClick method OnClick not called. Is it possible?

 <asp:Button OnClick="Button_Click" ID="Action1" OnClientClick="SaveValues()" Text="Action1" CssClass="specialButton" runat="server" /> 
+3
source share
3 answers

To block OnClick execution, just return false from OnClientClick :

 <asp:Button OnClick="Button_Click" ID="Action1" OnClientClick="return SaveValues();" Text="Action1" CssClass="specialButton" runat="server" /> 

Assuming SaveValues will also return a boolean, whether the condition was met.

+4
source

yes maybe

 function SaveValues() { if(//check your codition here) { return true; } else { return false; } } <asp:Button OnClick="Button_Click" ID="Action1" OnClientClick="javascript:return SaveValues();" Text="Action1" CssClass="specialButton" runat="server" /> 
+2
source

Returning true or false , in accordance with the success of JavaScript, should disable or enable the button for sending. Thus, instead of just pasting it at the end of OnClientClick , where you have to condition it and immediately make your call uglier, let this method indicate its working status.

+1
source

Source: https://habr.com/ru/post/949153/


All Articles