ASP.NET GridView Delete
The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. The following tutorial shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.
In this article I have used SQL SERVER database for sample data.
Before you start to generate GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.
To enable delete , set the AutoGenerateDeleteButton to true and specify the delete command in the SqlDataSource.
DeleteCommand="DELETE From [tblRecord] WHERE [Id] = @Id"
Here we are trying to display a confirmation message before deleting the specified row. In order to doing this we have to write a small Javascript code for display confirmation message.
function isDelete()
{
return confirm("Do you want to delete this row ?");
}
We have to call this isDelete() Javascript function on OnClientClick event of the delete LinkButton.
The follwoing ASP.NET program shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.
Gridview.aspx
Gridview.aspx.cs
In this article I have used SQL SERVER database for sample data.
Create table tblRecord ( Id int primary key, Name varchar(200),Branch varchar(50) ) --Insert some record
Before you start to generate GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.
Web.Config File
To enable delete , set the AutoGenerateDeleteButton to true and specify the delete command in the SqlDataSource.
DeleteCommand="DELETE From [tblRecord] WHERE [Id] = @Id"
Here we are trying to display a confirmation message before deleting the specified row. In order to doing this we have to write a small Javascript code for display confirmation message.
function isDelete()
{
return confirm("Do you want to delete this row ?");
}
We have to call this isDelete() Javascript function on OnClientClick event of the delete LinkButton.
The follwoing ASP.NET program shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.
Gridview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Girdview.aspx.cs" Inherits="Girdview" %> Gridview Demo
Gridview.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Girdview : System.Web.UI.Page { protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string cellID = e.Row.Cells[0].Text; LinkButton deleteButton = (LinkButton)e.Row.Cells[3].FindControl("DeleteBtn"); if (deleteButton != null) { deleteButton.Attributes.Add("onclick", "return isDelete();"); } } } }