ASP.NET GridView Delete Confirmation

GridView Displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items.

In this post, I will show you how to create a confirmation message with a Gridview when AutogenerateDelete="true"

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewDelete.aspx.cs" Inherits="GridViewDelete" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
  <title>Untitled Page</title>
  <form id="form1" runat="server">
          <asp:GridView ID="grdTest" runat="Server" AutoGenerateColumns="true" AutoGenerateDeleteButton="true"
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class GridViewDelete : System.Web.UI.Page
   protected void Page_Load(object sender, EventArgs e)

       //Check For postback
       if (!IsPostBack)

   protected void BindGrid()

       List<Customer> customers = new List<Customer>();

       Customer cust1 = new Customer(1, "Cust 1");
       Customer cust2 = new Customer(2, "Cust 2");
       Customer cust3 = new Customer(3, "Cust 3");


       grdTest.DataSource = customers;

   protected void grdTest_RowDataBound(object sender, GridViewRowEventArgs e)
       foreach (Control control in e.Row.Cells[0].Controls)
           LinkButton DeleteButton = control as LinkButton;
           if (DeleteButton != null && DeleteButton.Text == "Delete")
               DeleteButton.OnClientClick = "return(confirm('Are you sure you want to delete this record?'))";

public class Customer
   public Customer(int id, string name)
       ID = id;
       Name = name;

   private int m_id;

   public int ID
       get { return m_id; }
       set { m_id = value; }

   private string m_name;

   public string Name
       get { return m_name; }
       set { m_name = value; }
Next Post Previous Post
No Comment
Add Comment
comment url