Using JavaScript confirm() in ASP.NET control OnClientClick property
April 23rd, 2007 by Ivan Uzunov
Have you tried using JavaScript confirm() function in ASP.NET Button, LinkButton or ImageButton client onclick property?
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName=’DeleteItem’ Text=’Delete’ OnClientClick="confirm (’Are you sure you want to\ndelete this item?’);" ></asp:LinkButton>
But even if you click OK in the confirm dialog the page is not submited because the next JavaScript code for submitting the page (like WebForm_DoPostBackWithOptions) is not executed. The solution is using:
if(!confirm(‘your text’)) return false;
Instead of the code above I use:
ASP.NET, confirm(), JavaScript, OnClientClick<asp:LinkButton ID="lbtnDelete" runat="server" CommandName=’DeleteItem’ Text=’Delete’ OnClientClick="if(!confirm (’Are you sure you want to\ndelete this item?’))return false;" ></asp:LinkButton>




It seems counterintuitive to use
if(! confirm(…) ) return false;
I would prefer to use
if(confirm(…) ) return true;
or even
return confirm(…);
But those options do not work. I tried.
Thank you for your solution. It did work.
Very useful!!! Fabio
you’ve solved my problem, Stefano.
Thank you Ivan
Thank you Ivan for short usefull tip..