Showing Confirmation Dialog Using JavaScript

Introduction

Today we'll discuss some important aspects of the DataGrid in the ASP.NET. The DataGrid helps you to manipulate the data with select, edit, cancel and delete buttons. The buttons are associated with their events on the server side. The buttons have the command name and property for the select, edit or delete operations.

In that context, in here we'll work with the delete button. You have seen many times we need to get the confirmation from the user about deleting the record. DataGrid does not provide any builtin way to do that. We'll use the JavaScript to do the task.

Let's create the application using the following procedure:

  • Create the application
  • Working With DataGrid
  • Applying JavaScript
  • Run the application

Create the Application

In this section we'll create the simple empty application to work with the DataGrid. Use the following procedure.

Step 1: Open Visual Studio and Create the Web application.

Step 2: Select the empty project template.

Create Empty Project Template

Visual Studio automatically creates the application in the empty project template and adds references, files and folders to the project.

Working with DataGrid

Now to work with the DataGrid, we need bind it to the table of a database. Generate the database or you can also connect with the existing database. The DataGrid has a column Delete that triggers the DeleteCommand event handler. Use the following procedure to do that.

Step 1: Add a WebForm and using the following markup design the DataGrid:

  1. <asp:DataGrid ID="DtaGridView" AutoGenerateColumns="False" runat="server" Height="221px" Width="81px"   
  2. OnItemDataBound="DtaGridView_ItemDataBound" DataSourceID="SqlDataSource2">  
  3.     <Columns>  
  4.         <asp:TemplateColumn HeaderText="ID">  
  5.             <ItemTemplate>  
  6.                 <asp:Label ID="Lblid" runat="server" Text='<%#Bind("ID") %>'></asp:Label>  
  7.             </ItemTemplate>  
  8.         </asp:TemplateColumn>  
  9.         <asp:TemplateColumn HeaderText="Name">  
  10.             <ItemTemplate>  
  11.                 <asp:Label ID="Lblname" runat="server" Text='<%#Bind("Name") %>'></asp:Label>  
  12.             </ItemTemplate>  
  13.          </asp:TemplateColumn>  
  14.         <asp:TemplateColumn HeaderText="Contact">  
  15.             <ItemTemplate>  
  16.                 <asp:Label ID="Lblcontact" runat="server" Text='<%#Bind("Contact") %>'></asp:Label>  
  17.             </ItemTemplate>  
  18.         </asp:TemplateColumn>  
  19.         <asp:TemplateColumn HeaderText="Email">  
  20.             <ItemTemplate>  
  21.                 <asp:Label ID="Lblemail" runat="server" Text='<%#Bind("Email") %>'></asp:Label>  
  22.             </ItemTemplate>  
  23.         </asp:TemplateColumn>  
  24.         <asp:TemplateColumn>  
  25.             <ItemTemplate>  
  26.                 <asp:LinkButton ID="LbtnDelete" runat="server" Text="Delete" CausesValidation="false"   
  27. CommandName="Delete"></asp:LinkButton>  
  28.             </ItemTemplate>  
  29.         </asp:TemplateColumn>  
  30.     </Columns>  
  31. </asp:DataGrid> 

In the code above, I've bound the table to the DataGrid and there are four table fields added from the database. A linkbutton is also added as in a Template column named Delete button that has the CommandName as Delete.

Step 2: Select the properties of the DataGrid and make a double-click event for the ItemDataBound as in the following:

DataGrid Properties in ASP.NET

Step 3: Enter the following code in the ItemDataBound event of DataGrid:

  1. protected void DtaGridView_ItemDataBound(object sender, DataGridItemEventArgs e)  
  2. {  
  3.     LinkButton lb;  
  4.     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
  5.     {  
  6.         lb = (LinkButton)e.Item.Cells[0].FindControl("LbtnDelete");  
  7.         lb.Attributes.Add("onclick""return ConfirmDelete();");  
  8.     }  
  9. } 

In the code above, we are checking whether the row item is a type item or an alternating item. The DHTML OnClick event handler is used so that the client-side function named ConfirmDelete() is called.

Applying JavaScript

In this section, we'll add the JavaScript code to the web form so that the ConfirmDelete() is to be called when a user clicks on the Delete button. So just add the following markup in the <head> section:

  1. <script type="text/javascript">  
  2.     function ConfirmDelete() {  
  3.         if(confirm("Are you sure to want to Delete?")==true)  
  4.            return true;  
  5.         else  
  6.             return false;  
  7.     }  
  8. </script> 

The code above display the confirmation dialog box to the user with which the user can choose Ok to delete the data. If the user clicks on OK then the function returns true and the form is posted back as usual otherwise it returns false and the event is to be canceled and it will cancel the postback of the form also.

Run the Application

Now its time to run the application.

Step 1: Run the application and click on the Delete button of any row as in the following:

Deletion in Data Grid

Note: I've formatted my DataGrid.

 Step 2: The Delete confirmation dialog opens and then you can now choose the Ok can Cancel.

Delete Confirmation Dialog in Data Grid

That's it for now.

Summary

This article described how to display a confirmation dialog while deleting any row from the DataGrid in the ASP.NET. Thanks for reading.


Similar Articles