I have an asp:Button
on my webpage, and it calls into a JavaScript function and a code-behind method. The latter makes a call to navigate to another page. In the JavaScript function, I'm checking for a condition. If this condition is not met, I want to abort the navigation so that the OnClick
method is not called. Is this possible?
<asp:Button OnClick="Button_Click" ID="Action1" OnClientClick="SaveValues()"
Text="Action1" CssClass="specialButton" runat="server" />
yes it is possible
To block
OnClick
from executing, just returnfalse
fromOnClientClick
:Assuming that
SaveValues
will also return a boolean whether or not the condition was met.Returning
true
orfalse
, according to the success of the JavaScript, should prevent or enable the button to submit. So, instead of just tacking it on at the end ofOnClientClick
, where you'd have to conditionalise it and immediately make your call look much more ugly, let the method indicate its operational status.