Row deletion in a GridView by default does not provide
confirmation before deleting. Using a client-side verification message can
prevent accidental deletions. In this example, we will see how to utilize client-side
script to confirm deletions when using a GridView control.
In ASP.NET 2.0, the Button, LinkButton, and ImageButton controls
have an OnClientClick property. We can use this property to specify the
CommandField property of the GridView control unfortunately does not have an OnClientClick
property, but adding a button to the GridView can do the trick for us.
In this example, we will see how to use ASP.NET 2.0's
OnClientClick property for the Button control with the GridView control to show
a confirmation message before the user deletes the row.
Set Up the GridView
We will the use the GridView's TemplateField, containing an
ItemTemplate, which in turn contains a Button control. Such a button, with the
CommandName set to Delete, will cause the GridView to delete the associated row
when pressed. Since our aim is to show a confirmation message before the deletion
of the row, we will also make use of the button's OnClientClick property.
The button control's properties are assigned as follows:
CommandName to Delete
The DataKeyNames property of the GridView control is used to
specify the field or fields that represent the primary key of the data source.
Listing 1 – Display Data Using GridView Control
<asp:Button ID="btnDelete" Text="Delete" runat="server"
OnClientClick="return confirm('Are you sureyou want to delete this record?');"
<asp:BoundField HeaderText="EmployeeId" DataField="EmployeeId" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
Next, we set the SqlDataSource DeleteCommand and its
parameters. EmployeeId is the primary key in the Employees table. This key is
used to delete the row. The GridView needs to find which row has to be deleted,
hence we used @original_EmployeeId and not @EmployeeId. If we don't prefix this
parameter with original_ then there is no way to find that row which has to be
Listing 2 – The SqlDataSource
SelectCommand="Select * from Employees"
DeleteCommand="Delete from Employees whereEmployeeId=@original_EmployeeId"
<asp:Label ID="lblShow"runat="server" ForeColor="DarkRed"></asp:Label>
To show a message to the end user after the row has been
deleted we will use the Deleted event of the SqlDataSource control to do this
task for us and also supply and error messages.
Listing 3 - The Deletion Code
Visual Basic .NET
Protected Sub SqlDataSource1_Deleted(ByVal sender AsObject, _
ByVal e AsSystem.Web.UI.WebControls.SqlDataSourceStatusEventArgs)
If e.Exception Is Nothing Then
If e.AffectedRows = 1 Then
lblShow.Text = "Record deletedsuccessfully."
lblShow.Text = "An error occurred duringthe delete operation."
lblShow.Text = "An error occurred whileattempting to delete the row."
e.ExceptionHandled = True
protected void SqlDataSource1_Deleted(objectsender,
if (e.Exception == null)
if (e.AffectedRows == 1)
lblShow.Text = "Record deletedsuccessfully.";
lblShow.Text = "An error occurredduring the delete operation.";
lblShow.Text = "An error occurred whileattempting to delete the row.";
e.ExceptionHandled = true;
Figure 1 - GridView Before Deletion
Figure 2 - GridView After Deletion
In this article you learned how to integrate a confirmation
message box before deleting a row in a GridView, with a help of an example and