Display Confirmation message while deleting data in gridview

Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 1598
We will know how to Display Confirmation message while deleting data in gridview.

Create table student (sid varchar(50),sname varchar(50))

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

<!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>
<form id="form1" runat="server">
<asp:GridView DataKeyNames="sid" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">

<asp:BoundField DataField="sid" HeaderText="sid" />

<asp:BoundField DataField="sname" HeaderText="sname" />

<asp:TemplateField HeaderText="Select">


<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("sid") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>




using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)

private void BindGrid()
con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "Select * from student";
GridView1.DataSource = cmd.ExecuteReader();
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record:: " + DataBinder.Eval(e.Row.DataItem, "sid") + "')");
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
string sid = (string)GridView1.DataKeys[e.RowIndex].Value;
SqlConnection con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "DELETE FROM student WHERE sid='" + sid + "'";

Comments or Responses

Login to post response