How to confirm before uninstalling 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 confirms that before deletion (Yes / No). If so, delete the entry, and if not, do nothing.

For btnMainDelete, I put it like this:

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

Now the problem is that I click "Yes" or "No" always performs btnMainDelete_Click on the server side? I must have something missing here.

thanks

+4
source share
5 answers

Enter a valid script confirmation in OnClientClick:

 <asp:Button ID="btnMainDelete" OnClientClick="javascript:return confirm('Are you sure?');" runat="server" Text="Delete" OnClick="btnMainDelete_Click" /> 
+9
source

You need to fix your script. In a written script, it never prevents further script execution by returning false.

try:

 return confirm('Are you sure you want to delete this?'); 

IE:

 btnMainDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this?');"); 
+7
source

you want to change your code with

 <asp:Button ID="btnMainDelete" runat="server" Text="Delete" OnClientClick ="javascript:confirm('Are you sure want to delete ?');" /> 

OnClientClick: Gets or sets the client side of the script that runs when the Click Button control raises an event.

OnClick: Raises the Click event of the Button control

So, in your case, confirm it should raise the client side of the script. Therefore, you must specify the OnClientClick event.

+1
source

Put your script in OnClientClick

eg,

 btnMainDelete.OnClientClick = "if (confirm('Are you sure you want to delete this?') == false) return false;"; 
0
source

using this

 btnDelete.Attributes.Add("onclick", "return confirm('Do you really want to delete this item?' 

This will permanently delete the item in your database! '); ");

0
source

All Articles