Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

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

Comments and Discussions

 
QuestionNice article Pinmemberdotnetpickles12-Feb-14 18:04 
GeneralMy vote of 5 Pinmemberyatiji28-Apr-13 22:40 
GeneralCHECK THIS OUT - Easiest method so far packaged as "ConfirmButtonField" PinmemberJose Mesona26-Aug-09 18:04 
GeneralRe: Easily add confirm before delete in gridview Pinmemberdeepak_rai16-Sep-08 4:57 
GeneralDelete is performing even though click no Pinmembernareshu11-Jun-08 23:04 
GeneralRe: Delete is performing even though click no [modified] PinmemberDavidRRR10-Aug-08 17:40 
GeneralAutoGenerateDeleteButton PinmemberProp Top27-Sep-07 4:34 
GeneralRe: AutoGenerateDeleteButton PinmemberYves Tr29-Oct-07 9:15 
GeneralRe: AutoGenerateDeleteButton Pinmemberohyeahbaby14-May-09 5:21 
GeneralRe: AutoGenerateDeleteButton Pinmemberlilesh30-Oct-09 22:26 
Generala quandry from sephoratrading PinadminSean Ewington1-Jun-07 8:56 
GeneralClientClick Pinmemberleppie25-May-07 5:06 
GeneralAlternative method PinmemberMetAK23-May-07 21:22 
GeneralRe: Alternative method PinmemberLeo Muller23-May-07 22:51 
GeneralRe: Alternative method Pinmemberdaerath23-Oct-07 4:14 
GeneralRe: Alternative method PinmemberPazout15-Feb-09 10:37 
Generali done Pinmemberjariaman14-May-07 21:35 
GeneralRe: i done PinmemberLeo Muller23-May-07 22:54 
GeneralgvTenders [modified] Pinmemberjariaman14-May-07 21:14 
Questionwhy static method? Pinmemberbug menot12-Mar-07 8:23 
AnswerRe: why static method? PinmemberLeo 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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 24 May 2007
Article Copyright 2007 by Leo Muller
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid