![]() |
Web Development »
ASP.NET »
Howto
Intermediate
License: The Code Project Open License (CPOL)
Simple (non-JS) GridView Delete ConfirmationBy Daniel MorrisA simple server-side solution for displaying a confirmation message upon Deleting an item in a Grid View |
C# (C# 2.0, C# 3.0), WebForms, Dev
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
A common requirement in non-trivial data editing forms is to request confirmation of pending deletions. However, the default behaviour of the .NET GridView control on deletion is to commit the deletion without soliciting any further feedback from the user. This behaviour does tend to make GridView-based data editing forms prone to accidental deletion ("I swear pressed the 'Edit' link...").
Numerous solutions to the above problem have been documented. The approach discussed here is a simple server-side solution which doesn't require the use of Javascript popups.
An example screenshot of the approach in action is shown below, with the third record (TaskID 4) displaying the Confirm link following the use of the Delete link.

The server-side Deletion confirmation approach involves: (i) the use of explicit Command links for Edit and Delete, and (ii) implementation of the gridview_RowDeleting event handler.
Rather than using a GridView CommandField for rendering the Edit and Delete Command links, templated LinkButtons are used instead; one LinkButton for Edit (with a CommandName of 'Edit') and another LinkButton for Delete (with a CommandName of 'Delete'). The use of explicit controls here allows us to manipulate the Command links server-side on post-back. Note that the remainder of the GridView mark-up has been omitted from the sample provided below.
// rather than use a CommandField, i.e... <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" /> // ...explicitly list each command as a LinkButton in a Template Field instead <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit" /> <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update" /> <asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> </asp:TemplateField>
The RowDeleting event handler needs to accomplish two tasks:
1) Show a Confirm link (plus an associated Cancel link) if the Delete Command link has been pressed (i.e., on first transit of a GridView item through the RowDeleting event handler).
2) Delete the GridView item if the Confirm Command link has been pressed (i.e., on second transit of a GridView item through the RowDeleting event handler).
The approach used here is a bit lazy, but avoids complicating the solution with additional complexity for what is a fairly straightforward process, which goes something like this:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { // Grab reference to the Delete Command link. LinkButton lnk = (LinkButton)GridView1.Rows[e.RowIndex].FindControl("lnkDelete"); // If the Command link text is 'Delete', display a 'Confirm' link. if (lnk.Text == "Delete") { lnk.Text = "Confirm"; e.Cancel = true; // Don't want to delete just yet. Need confirmation first. LinkButton lnk2 = (LinkButton)GridView1.Rows[e.RowIndex].FindControl("lnkEdit"); lnk2.Text = "Cancel"; lnk2.CommandName = "Cancel"; } // Otherwise, the Delete command will process to completion as normal. }
The above solution provides a workable approach to implementing Delete Confirm functionality in a GridView. One possible embellishment to make the solution a little more user-friendly would be to change the CssClass of the pending deletion row to make it a bit more obvious that (i) something important is about to happen, and (ii) further action is required by the user.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 16 May 2008 Editor: |
Copyright 2008 by Daniel Morris Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |