Click here to Skip to main content
6,822,123 members and growing! (16,020 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Easily add confirm before delete in gridview

By Leo Muller

Easily add confirmation code before deleting row in gridview, from code behind
C#2.0, Windows, .NET2.0, ASP.NET, WebForms, VS2005, Dev
Posted:12 Mar 2007
Updated:23 May 2007
Views:39,448
Bookmarked:31 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 2.45 Rating: 2.56 out of 5
3 votes, 33.3%
1
1 vote, 11.1%
2

3
2 votes, 22.2%
4
3 votes, 33.3%
5

Introduction

Often you want to add a confirmation for the user, before deleting a row with the gridview control. This can of course easily be done by placing a link in an itemtemplate, and adding the correct value in the onclientclick property. But if you want / have to do this from code behind. A dependency on your cell index, will eventually be overseen, with possible disastrous results.

Background

you may face a problem. Even though it can easily be done through the Cells collection, one day, someone is going to add / move / remove a column, without looking in the code behind.

Using the code

The code below will demonstrate how this can be done with a method that will find the delete buttons automatically, and add the necessary code.

//


protected void gvBooks_RowDataBound(object sender, GridViewRowEventArgs e)
{
  AddConfirmDelete((GridView)sender, e);
}

/// <summary>

/// If the gridview has a command field where showdeletebutton is true, then 

/// it add a confirm message. 

/// This function should be called in the RowDataBound event

/// </summary>


public static void AddConfirmDelete(GridView gv, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  foreach (DataControlField dcf in gv.Columns)
  {
  if (dcf.ToString() == "CommandField")
   {
    if (((CommandField)dcf).ShowDeleteButton == true)
    {
     e.Row.Cells[gv.Columns.IndexOf(dcf)].Attributes
     .Add("onclick", "return confirm(\"Are you sure?\")");
    }
   }
  }
 }
}
//

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Leo Muller


Member

Location: Israel Israel

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralCHECK THIS OUT - Easiest method so far packaged as "ConfirmButtonField" PinmemberJose Mesona19:04 26 Aug '09  
GeneralRe: Easily add confirm before delete in gridview Pinmemberdeepak_rai5:57 16 Sep '08  
GeneralThe message displays on "Edit" button also Pinmemberrezatech8:15 11 Aug '08  
GeneralDelete is performing even though click no Pinmembernareshu0:04 12 Jun '08  
GeneralRe: Delete is performing even though click no [modified] PinmemberDavidRRR18:40 10 Aug '08  
GeneralAutoGenerateDeleteButton PinmemberProp Top5:34 27 Sep '07  
GeneralRe: AutoGenerateDeleteButton PinmemberYves Tr10:15 29 Oct '07  
Here what I am using. It's a derivate from the above code:

            /// <summary>
            ///      If the gridview has AutoGenerateDeleteButton set to true, then
            ///      it add a confirm message to the delete buttons.
            ///      This function should be called in the RowDataBound event
            /// </summary>
            protected static void AddConfirmDelete(GridView gv, GridViewRowEventArgs e)
            {
                  // Make sure we have the delete button showing.
                  if (gv.AutoGenerateDeleteButton != true)
                        return;

                  // Make sure we are processing a DataRow
                  if (e.Row.RowType != DataControlRowType.DataRow)
                        return;

                  // Search for the Delete button
                  foreach (DataControlFieldCell dcf in e.Row.Cells)
                  {
                        // The header for the LinkButton is normally empty.
                        if (dcf.Text == "")
                        {
                              // Search the list of control and file the Delete link
                              foreach (Control ctrl in dcf.Controls)
                              {
                                    LinkButton deleteButton = ctrl as LinkButton;
                                    if (deleteButton != null && deleteButton.Text == "Delete")
                                    {
                                          deleteButton.Attributes.Add("onClick", "return confirm('Are you sure you want to delete this row?');");
                                          break;
                                    }
                              }
                              break;
                        }
                  }
            }

GeneralRe: AutoGenerateDeleteButton Pinmemberohyeahbaby6:21 14 May '09  
GeneralRe: AutoGenerateDeleteButton Pinmemberlilesh23:26 30 Oct '09  
Generala quandry from sephoratrading PinadminSean Ewington9:56 1 Jun '07  
GeneralClientClick Pinmemberleppie6:06 25 May '07  
GeneralAlternative method PinmemberMetAK22:22 23 May '07  
GeneralRe: Alternative method PinmemberLeo Muller23:51 23 May '07  
GeneralRe: Alternative method Pinmemberdaerath5:14 23 Oct '07  
GeneralRe: Alternative method PinmemberPazout11:37 15 Feb '09  
Generali done Pinmemberjariaman22:35 14 May '07  
GeneralRe: i done PinmemberLeo Muller23:54 23 May '07  
GeneralgvTenders [modified] Pinmemberjariaman22:14 14 May '07  
Generalwhy static method? Pinmemberbug menot9:23 12 Mar '07  
GeneralRe: why static method? PinmemberLeo Muller12:23 13 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 23 May 2007
Editor:
Copyright 2007 by Leo Muller
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project