Javascript Confirm Delete Alert Message before Deleting from Database Asp.net C# VB.Net

In our Asp.Net web application a common task is to insert, update or delete data from Database. It will be a good practice if you prompt user before deleting a row or data from Database since after deletion you can not retrieve data from database. Think if user click to delete a row & you don’t prompt any confirm alert message then the data will be deleted. If the user click on delete Button mistakenly then what happen? So its a good idea to prompt user by using Javascript confirm before deleting a row.

Javascript_Confirm_delete_message_Before deleting_from_database_asp.net_Csharp_VB.net

As shown in above figure when a user clicks on a LinkButton or on a Button control system will prompt “Are you sure to delete?” javascript confirm message box with two button OK & Cancel. If user click on OK then corresponding server side code will be executed. If cancel then nothing happened.

One can prompt user by using 3 ways. You can use any one of LinkButton, Button Or Image Button control even within GridView, DataList or Repeater control. These are:

Way 1: Using Properties.
Way 2: From HTML Markup.
Way 3: Runtime or Programmatically.

Using Properties:
This is the most simple & easy way to prompt user confirmation message from LinkButton / Button / Image Button control property. Look at the below image how to do it.

Delete2
Note: First right click on any one of your control such as LinkButton / Button / Image Button. Then go to the properties. From properties find OnClientClick attribute and write the following code:

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

From HTML Markup:
Its also easy. One can easily add the confirmation message from HTML Markup language of the control in the following way:

    <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure to delete?'); "></asp:LinkButton>
    <asp:Button ID="cmdDelete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure to delete?'); " />

Now run the project hope you will experience same thing as liks as way #1.

Runtime or Programmatically:
To do that we need to re-arrange the above HTML markup like below:

    <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete"></asp:LinkButton>
    <asp:Button ID="cmdDelete" runat="server" Text="Delete"/>

Now under Page_load Event Add the below Code:
C# Code:

        if (!IsPostBack)
        {
            lnkDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');");
            cmdDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');");
        }

VB.Net Code:

        If Not IsPostBack Then
            lnkDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');")
            cmdDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');")
        End If

Now run the project hope you will experience same thing as liks as way #1 & #2.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Client, Javascript, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*