Delete button of the
GridView control in ASP.NET.
This might be an old trick but is helpful to beginner developers.
Consider the following ASPX grid code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="true"
<asp:BoundField DataField="EMPID" HeaderText="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Address" HeaderText="Address" />
As you can see in the above code, AutoGenerateDeleteButton="true" means in
the grid you can see a delete button to remove the record
from the grid. But the requirement is I have to ask the user whether he wants to delete
the record or not.
So to achieve this I made use of the
RowDataBound event and the
the user presses
yes then page gets a postback and it removes the record from the grid and marks it as deleted in
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState ==
"if(!confirm('Are you sure to delete this row?'))return false;";
As you can see in the above code I am searching for the deletelinkbutton and attaching
Because auto-generated buttons are always placed in cell 0 of the grid and here as you can see in
the code, the edit button is also auto generated which gets placed first
in cell 0 of the grid so the auto-generated delete button gets placed 2 in cell 0.