Click here to Skip to main content
Licence CPOL
First Posted 12 Mar 2007
Views 51,584
Bookmarked 33 times

Easily add a confirm message before a delete from a GridView

By | 23 May 2007 | Article
Easily add confirmation code before deleting a row in a GridView, from code-behind.

Introduction

Often you want to add a confirmation message for the user before deleting a row, say, from a 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 the code-behind, a dependency on your cell index will eventually be overseen, with possible disastrous results.

Background

Even though this can easily be done through the Cells collection, one day, someone is going to add / move / remove a column without looking at 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Leo Muller



Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCHECK THIS OUT - Easiest method so far packaged as "ConfirmButtonField" PinmemberJose Mesona18:04 26 Aug '09  
GeneralRe: Easily add confirm before delete in gridview Pinmemberdeepak_rai4:57 16 Sep '08  
GeneralThe message displays on "Edit" button also Pinmemberrezatech7:15 11 Aug '08  
GeneralDelete is performing even though click no Pinmembernareshu23:04 11 Jun '08  
GeneralRe: Delete is performing even though click no [modified] PinmemberDavidRRR17:40 10 Aug '08  
GeneralAutoGenerateDeleteButton PinmemberProp Top4:34 27 Sep '07  
GeneralRe: AutoGenerateDeleteButton PinmemberYves Tr9:15 29 Oct '07  
GeneralRe: AutoGenerateDeleteButton Pinmemberohyeahbaby5:21 14 May '09  
GeneralRe: AutoGenerateDeleteButton Pinmemberlilesh22:26 30 Oct '09  
Generala quandry from sephoratrading PinadminSean Ewington8:56 1 Jun '07  
GeneralClientClick Pinmemberleppie5:06 25 May '07  
GeneralAlternative method PinmemberMetAK21:22 23 May '07  
GeneralRe: Alternative method PinmemberLeo Muller22:51 23 May '07  
GeneralRe: Alternative method Pinmemberdaerath4:14 23 Oct '07  
GeneralRe: Alternative method PinmemberPazout10:37 15 Feb '09  
Generali done Pinmemberjariaman21:35 14 May '07  
GeneralRe: i done PinmemberLeo Muller22:54 23 May '07  
GeneralgvTenders [modified] Pinmemberjariaman21:14 14 May '07  
Questionwhy static method? Pinmemberbug menot8:23 12 Mar '07  
AnswerRe: why static method? PinmemberLeo Muller11:23 13 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 24 May 2007
Article Copyright 2007 by Leo Muller
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid