Click here to Skip to main content
11,643,493 members (70,127 online)
Click here to Skip to main content

Easily add a confirm message before a delete from a GridView

, 23 May 2007 CPOL 69.4K 34
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Leo Muller
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionNice article Pin
dotnetpickles12-Feb-14 18:04
memberdotnetpickles12-Feb-14 18:04 
GeneralMy vote of 5 Pin
yatiji28-Apr-13 22:40
memberyatiji28-Apr-13 22:40 
GeneralCHECK THIS OUT - Easiest method so far packaged as "ConfirmButtonField" Pin
Jose Mesona26-Aug-09 18:04
memberJose Mesona26-Aug-09 18:04 
GeneralRe: Easily add confirm before delete in gridview Pin
deepak_rai16-Sep-08 4:57
memberdeepak_rai16-Sep-08 4:57 
GeneralDelete is performing even though click no Pin
nareshu11-Jun-08 23:04
membernareshu11-Jun-08 23:04 
GeneralRe: Delete is performing even though click no [modified] Pin
DavidRRR10-Aug-08 17:40
memberDavidRRR10-Aug-08 17:40 
GeneralAutoGenerateDeleteButton Pin
Prop Top27-Sep-07 4:34
memberProp Top27-Sep-07 4:34 
GeneralRe: AutoGenerateDeleteButton Pin
Yves Tr29-Oct-07 9:15
memberYves Tr29-Oct-07 9:15 
GeneralRe: AutoGenerateDeleteButton Pin
ohyeahbaby14-May-09 5:21
memberohyeahbaby14-May-09 5:21 
GeneralRe: AutoGenerateDeleteButton Pin
lilesh30-Oct-09 22:26
memberlilesh30-Oct-09 22:26 
Generala quandry from sephoratrading Pin
Sean Ewington1-Jun-07 8:56
adminSean Ewington1-Jun-07 8:56 
GeneralClientClick Pin
leppie25-May-07 5:06
memberleppie25-May-07 5:06 
GeneralAlternative method Pin
MetAK23-May-07 21:22
memberMetAK23-May-07 21:22 
GeneralRe: Alternative method Pin
Leo Muller23-May-07 22:51
memberLeo Muller23-May-07 22:51 
GeneralRe: Alternative method Pin
daerath23-Oct-07 4:14
memberdaerath23-Oct-07 4:14 
GeneralRe: Alternative method Pin
Pazout15-Feb-09 10:37
memberPazout15-Feb-09 10:37 
Generali done Pin
jariaman14-May-07 21:35
memberjariaman14-May-07 21:35 
GeneralRe: i done Pin
Leo Muller23-May-07 22:54
memberLeo Muller23-May-07 22:54 
GeneralgvTenders [modified] Pin
jariaman14-May-07 21:14
memberjariaman14-May-07 21:14 
Questionwhy static method? Pin
bug menot12-Mar-07 8:23
memberbug menot12-Mar-07 8:23 
AnswerRe: why static method? Pin
Leo Muller13-Mar-07 11:23
memberLeo Muller13-Mar-07 11:23 

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.

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