Click here to Skip to main content
13,257,609 members (47,118 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 12 Mar 2007

Easily add a confirm message before a delete from a GridView

, 23 May 2007
Rate this:
Please Sign up or sign in to vote.
Easily add confirmation code before deleting a row in a GridView, from code-behind.


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.


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)
     .Add("onclick", "return confirm(\"Are you sure?\")");


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
No Biography provided

You may also be interested in...

Comments and Discussions

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

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