Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C#

Simple (non-JS) GridView Delete Confirmation

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
15 May 2008CPOL3 min read 29.9K   24   2
A simple server-side solution for displaying a confirmation message upon deleting an item in a GridView.

Introduction

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 I pressed the 'Edit' link...").

Background

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.

Image 1

Using the code

The server-side deletion confirmation approach involves:

  1. the use of explicit Command Links for Edit and Delete, and
  2. implementation of the gridview_RowDeleting event handler.

Command Links

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.

ASP.NET
// 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>

GridView RowDeleting Event Handler

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:

  • The 'Delete' CommandName of the Delete Command Link initiates row deletion on the GridView. The first event fired in the row deletion pipeline is the RowDeleting event. By handling this event, we have the opportunity to control the outcome of the deletion process (e.g., by cancelling the event), plus we can also update the GridView content as desired.
  • The actual deletion of a GridView item requires the item to transit the RowDeleting handler twice. On first transit, the text of the Delete Command Link (i.e., the LinkButton with the ID "lnkDelete") is updated from 'Delete' to 'Confirm', and further processing along the GridView item deletion pipeline is cancelled by setting the Cancel property of the incoming GridViewDeleteEventArgs argument to true. On second transit of the GridView item through the RowDeleting handler, we effectively recognize that the row deletion event was fired by a Confirm link by the simple fact that the text of the DeleteCommand link is not 'Delete' (but rather 'Confirm'). In this case, the deletion proceeds as normal.
  • To provide a means of cancelling the deletion, the otherwise redundant Edit Command Link is re-employed as a Cancel Command link by:
    1. Changing its text to 'Cancel', and
    2. changing it's CommandName to 'Cancel'.
C#
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.
}

Further Embellishments

The above solution provides a workable approach to implementing a 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:

  1. something important is about to happen, and
  2. further action is required by the user.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAn Alternative Solution (one line of JS required) Pin
GoodSyntax20-May-08 3:45
GoodSyntax20-May-08 3:45 
GeneralRe: An Alternative Solution (one line of JS required) Pin
Daniel Morris13-Aug-08 3:45
Daniel Morris13-Aug-08 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.