aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies Delete gridview records with confirmationbox example

Jan 1, 2011

Here I will explain how to delete records in gridview with confirmationbox message in


I designed gridview with linkbutton that contains user information details. Here my requirement is whenever user clicks on link button at that time I need to display confirmation message and if user clicks on ok button in confirmation box I want to delete record from database otherwise no action should perform on particular record. For that we need to follow below steps 

Design your aspx page like this 

<html xmlns="" >
<head runat="server">
<title>ConfirmBox in Gridview Row Delete</title>
<style type="text/css">

<script type="text/javascript">
function ConfirmationBox(username) {

var result = confirm('Are you sure you want to delete '+username+' Details' );
if (result) {

return true;
else {
return false;
<form id="form1" runat="server">
<asp:GridView runat="server" ID="gvrecords" CssClass="Gridview"
DataKeyNames="UserId" AutoGenerateColumns="false" 
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White"
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField HeaderText="Delete User Details">
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete User</asp:LinkButton>
After that write the following code in code behind

SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
protected void BindUserDetails()
//connection open
//sql command to execute query from database
SqlCommand cmd = new SqlCommand("Select * from UserDetails",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//Binding data to gridview
gvrecords.DataSource = ds;
protected void gvrecords_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
//getting username from particular row
string username =Convert.ToString(DataBinder.Eval(e.Row.DataItem,"UserName"));
//identifying the control in gridview
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
//raising javascript confirmationbox whenver user clicks on link button
lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + username+ "')");
protected void lnkdelete_Click(object sender, EventArgs e)

LinkButton lnkbtn= sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int userid = Convert.ToInt32(gvrecords.DataKeys[gvrow.RowIndex].Value.ToString());
string username = gvrow.Cells[0].Text;
SqlCommand cmd = new SqlCommand("delete from UserDetails where UserId="+userid,con);
int result=cmd.ExecuteNonQuery();
if (result == 1)
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + username + " details deleted successfully')", true);

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


vinay said...

Really Goodm, Easily understandable for Beginners

enGain said...

This code very useful for me.

thanks for your help.
easily understand.

by mohankumar.

Touseef said...

Thanks it is really helpful

Anonymous said...

i cant use datetime picker in master page

Anonymous said...

Thanks it is really helpful

Unknown said...

Thank you, this article is really very helpful.

Anonymous said...

Thaks this code is work for me

Unknown said...

I've spent half a day trying to find how to do this exact thing, and it is perfect. Easy to understand even for novices such as myself. Thank you for taking the time to put this together and share with others!!

Anil said...

Thanks for your wonderful article. Its really saved my time and help me lot!.

Anonymous said...

thanks this gr8 article .its really helpful article

Anonymous said...

In Gridview I Have Selected Auto Generate Delete Button ......So How To Apply This Code In That........

Manoj B. Kalla said...

Very good . . .sureshbhai

Unknown said...

how to use link button in gridview. Is it possible?.. Plz any one explain..

Unknown said...

Thynks...its really very helpfull..

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.