Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm postback OnClientClick button ASP.NET

<asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton"                            OnClick="BtnUserDelete_Click"                            OnClientClick="return UserDeleteConfirmation();"   meta:resourcekey="BtnUserDeleteResource1" /> 

I have tried:

function UserDeleteConfirmation() {         if (confirm("Are you sure you want to delete this user?"))             return true;         else             return false; } 

and

function UserDeleteConfirmation() {     if (confirm("Are you sure you want to delete this user?")) {             __doPostBack(btnUserDelete, '');     }      return false;  } 

And none of them works.

like image 958
makambi Avatar asked Dec 27 '12 16:12

makambi


1 Answers

Try this:

<asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton"                        OnClick="BtnUserDelete_Click"                        OnClientClick="if ( ! UserDeleteConfirmation()) return false;"   meta:resourcekey="BtnUserDeleteResource1" /> 

This way the "return" is only executed when the user clicks "cancel" and not when he clicks "ok".

By the way, you can shorten the UserDeleteConfirmation function to:

function UserDeleteConfirmation() {     return confirm("Are you sure you want to delete this user?"); } 
like image 114
Hans Kesting Avatar answered Oct 13 '22 20:10

Hans Kesting