kacalapy
kacalapy

Reputation: 10154

asp.net button click w/ javascript "are you sure?" prior to post back

i have a asp:button that will fire a delete and want to have a client side javascript are you sure pop-up prevent any accidents.

whats the javascript to handle this?

Upvotes: 10

Views: 8169

Answers (2)

MobileMon
MobileMon

Reputation: 8661

This worked for me (the accepted answer did not)

<asp:Button id="DeleteButton" runat="server" Text="Delete" 
OnClientClick="if (!confirm('Are you sure you want to delete?')) return false;">
</asp:Button>

Upvotes: 0

James King
James King

Reputation: 6343

You can add the javascript to the OnClientClick() event of the button... the key is to return false if you want to cancel the event. If you return false, the OnClick will not fire.

<asp:Button id="DeleteButton" runat="server" Text="Delete" 
            OnClick ="delete_clickhandler" 
            OnClientClick="return confirm('Are you sure you want to?');" /> 

Alternately, you can call a method in javascript

<asp:Button id="DeleteButton" runat="server" Text="Delete" 
            OnClick ="delete_clickhandler" 
            OnClientClick="return MyDeleteConfirm();" /> 

Where MyDeleteConfirm() does something more elaborate, but returns false if you don't want to delete.

Upvotes: 18

Related Questions