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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdTest" runat="Server" AutoGenerateColumns="true" AutoGenerateDeleteButton="true"
OnRowDataBound="grdTest_RowDataBound">
</asp:GridView>
</div>
</form>
</body>
</html>
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)
{
BindGrid();
}
}
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");
customers.Add(cust1);
customers.Add(cust2);
customers.Add(cust3);
grdTest.DataSource = customers;
grdTest.DataBind();
}
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; }
}
}