Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to to confirming before deleting in ASP.NET?

I have code like this in Page_Load()

btnMainDelete.Attributes.Add("onclick", "if(confirm('Are you sure you want to delete this?')){}else{return true}");

Basically, it's confirming that before deletion (Yes/No). If it's a yes then delete the record and if it's no then do nothing.

For the btnMainDelete, I put as follow:

<asp:Button ID="btnMainDelete" runat="server" Text="Delete" OnClick="btnMainDelete_Click" />

Now the issue is that, what I press Yes or No always executes btnMainDelete_Click on the server side? I must have something missing here.

Thanks

like image 888
user125108 Avatar asked Dec 04 '22 14:12

user125108


1 Answers

Put valid confirm script in the OnClientClick:

<asp:Button ID="btnMainDelete" OnClientClick="javascript:return confirm('Are you sure?');" runat="server" Text="Delete" OnClick="btnMainDelete_Click" />
like image 160
rick schott Avatar answered Jan 04 '23 09:01

rick schott