This article snippet explains how to delete data from GridView
using template buttons.
Now we look at how to add template button in GridView
. First select Edit Columns
in GridView
.
Second, add Template Column
in GridView
.
Now configure Template Column
, click Edit Template
. Place a LinkButton
on Template
field. Click Edit Databindings
, then select CommandArgument
, after that, set field binding, bound to "ID
" field, this ID
field is used to delete data in server side code. Then press ok button.
Now we can configure LinkButton
[Delete button].
Select Property
of Link button. Set CommandName
and Text as Delete. We can access this CommandName
in GridView1_RowCommand
event. After this, add GridView
events GridView1_RowCommand
and GridView1_RowDeleted.
The RowDeleted
event is raised whenever a Delete button associated with an item in the GridView
control is clicked, but after the GridView
control deletes the record.
This allows you to provide an event-handling method that performs a custom routine, such as checking the results of a delete operation, whenever this event occurs. To avoid errors, we add one custom code in GridView1_RowDeleted
event.
e.ExceptionHandled = true;
Now the server side part.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
e.ExceptionHandled = true;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "dele")
{
DBHelper objDBHelper = new DBHelper();
string sqlCmdText = string.Format
("DELETE FROM Table WHERE ID='{0}'", e.CommandArgument.ToString());
objDBHelper.ExecuteScalar(sqlCmdText);
GridView1.DataBind();
}
}
}
ASPX Page:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
onrowdeleted="GridView1_RowDeleted">
<Columns>
<asp:BoundField DataField="biInvitationId" HeaderText="biInvitationId"
InsertVisible="False" ReadOnly="True"
SortExpression="biInvitationId" />
<asp:BoundField DataField="biEventName" HeaderText="biEventName"
SortExpression="biEventName" />
<asp:BoundField DataField="biHostName" HeaderText="biHostName"
SortExpression="biHostName" />
<asp:BoundField DataField="biTelephone" HeaderText="biTelephone"
SortExpression="biTelephone" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("biInvitationId") %>'
CommandName="dele">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Thank you!
Feel free to comment.
CodeProject