Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent form postback on <asp:Button> using a jQuery confirmation dialog

I have an asp.net button with both onclick and onclientclick events.

<asp:Button  ID="ButtonSave" Text="Save" ToolTip="Save" CssClass="causesValidation button" OnClick="ButtonSave_Click" OnClientClick="myfunction();" runat="server"/>

As soon as I click the button, both the client side and server side events are being fired. I only want the server side event to run if the client-side function succeeds.

I tried the following:

function myfunction()
{
    // Show dialog
    $('#mydialog').dialog('open');


// on dialog confirmation return true, else false;
}

<asp:Button  ID="ButtonSave" Text="Save" ToolTip="Save" CssClass="causesValidation button" OnClick="ButtonSave_Click" OnClientClick="return myfunction();" runat="server"/>

Thank you

like image 523
user1473171 Avatar asked Jan 01 '26 05:01

user1473171


1 Answers

Change

OnClientClick="myfunction();" 

by

OnClientClick="return myfunction();" 
like image 166
Claudio Redi Avatar answered Jan 03 '26 08:01

Claudio Redi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!