Confirm postback OnClientClick button ASP.NET
Solution 1:
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?");
}
Solution 2:
There are solutions here that will work, but I don't see anyone explaining what is actually happening here, so even though this is 2 years old I'll explain it.
There is nothing "wrong" with the onclientclick javascript you are adding. The problem is that asp.net is adding it's on onclick stuff to run AFTER whatever code you put in there runs.
So for example this ASPX:
<asp:Button ID="btnDeny" runat="server" CommandName="Deny" Text="Mark 'Denied'" OnClientClick="return confirm('Are you sure?');" />
is turned into this HTML when rendered:
<input name="rgApplicants$ctl00$ctl02$ctl00$btnDeny" id="rgApplicants_ctl00_ctl02_ctl00_btnDeny"
onclick="return confirm('Are you sure?');__doPostBack('rgApplicants$ctl00$ctl02$ctl00$btnDeny','')" type="button" value="Mark 'Denied'" abp="547">
If you look closely, the __doPostBack stuff will never be reached, because the "confirm" will always return true/false before __doPostBack is reached.
This is why you need to have the confirm only return false and not return when the value is true. Technically, it doesn't matter if it returns true or false, any return in this instance would have the effect of preventing the __doPostBack from being called, but for convention I would leave it so that it returns false when false and does nothing for true.