Click here to Skip to main content
15,885,244 members
Articles / Web Development / ASP.NET

Easily add a confirm message before a delete from a GridView

Rate me:
Please Sign up or sign in to vote.
2.83/5 (11 votes)
23 May 2007CPOL 100.4K   35   22
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:

C#
//
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)


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

Comments and Discussions

 
QuestionNice article Pin
dotnetpickles12-Feb-14 18:04
dotnetpickles12-Feb-14 18:04 
GeneralMy vote of 5 Pin
yatiji28-Apr-13 22:40
yatiji28-Apr-13 22:40 
GeneralCHECK THIS OUT - Easiest method so far packaged as "ConfirmButtonField" Pin
Jose Mesona26-Aug-09 18:04
Jose Mesona26-Aug-09 18:04 
GeneralRe: Easily add confirm before delete in gridview Pin
deepak_rai16-Sep-08 4:57
deepak_rai16-Sep-08 4:57 
GeneralThe message displays on "Edit" button also Pin
rezatech11-Aug-08 7:15
rezatech11-Aug-08 7:15 
GeneralDelete is performing even though click no Pin
nareshu11-Jun-08 23:04
nareshu11-Jun-08 23:04 
Hi Leo,

I copied the code and i kept in my project. it is prompting message box, but the rows are getting deleted in either i press yes or no. Please help me. its urgent.

Naresh Kumar .P
Happy coding.

GeneralRe: Delete is performing even though click no [modified] Pin
DavidRRR10-Aug-08 17:40
DavidRRR10-Aug-08 17:40 
GeneralAutoGenerateDeleteButton Pin
Prop Top27-Sep-07 4:34
Prop Top27-Sep-07 4:34 
GeneralRe: AutoGenerateDeleteButton Pin
Yves Tr29-Oct-07 9:15
Yves Tr29-Oct-07 9:15 
GeneralRe: AutoGenerateDeleteButton Pin
ohyeahbaby14-May-09 5:21
ohyeahbaby14-May-09 5:21 
GeneralRe: AutoGenerateDeleteButton Pin
lilesh30-Oct-09 22:26
lilesh30-Oct-09 22:26 
Generala quandry from sephoratrading Pin
Sean Ewington1-Jun-07 8:56
staffSean Ewington1-Jun-07 8:56 
GeneralClientClick Pin
leppie25-May-07 5:06
leppie25-May-07 5:06 
GeneralAlternative method Pin
MetAK23-May-07 21:22
MetAK23-May-07 21:22 
GeneralRe: Alternative method Pin
Leo Muller23-May-07 22:51
Leo Muller23-May-07 22:51 
GeneralRe: Alternative method Pin
daerath23-Oct-07 4:14
daerath23-Oct-07 4:14 
GeneralRe: Alternative method Pin
Pazout15-Feb-09 10:37
Pazout15-Feb-09 10:37 
Generali done Pin
jariaman14-May-07 21:35
jariaman14-May-07 21:35 
GeneralRe: i done Pin
Leo Muller23-May-07 22:54
Leo Muller23-May-07 22:54 
GeneralgvTenders [modified] Pin
jariaman14-May-07 21:14
jariaman14-May-07 21:14 
Questionwhy static method? Pin
Thanks for all the fish12-Mar-07 8:23
Thanks for all the fish12-Mar-07 8:23 
AnswerRe: why static method? Pin
Leo Muller13-Mar-07 11:23
Leo Muller13-Mar-07 11:23 

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.