This is a basic logic needed in Grid-view while deleting a data. By mistake people may click
on the delete button and if there is no alert or confirmation, some valuable data may be lost.
Let's go for implementation, below is the grid-view
<asp:GridView ID="GridView2" runat="server" AutoGenerateSelectButton="true"
<asp:BoundField DataField="ID" HeaderText="SL.No" Visible="false"/>
<asp:BoundField DataField="Project" HeaderText="Project" Visible="false"/>
In the above grid the two properties namely AutoGenerateSelectButton,
and AutoGenerateDeleteButton let you show the Select and
Delete button on the GridView.
Now on onrowdatabound event of the GridView we implement the logic like
protected void GridView2_RowDataBound(object sender,
// Gets the Delete command column, which is the first column
foreach (Control control in e.Row.Cells.Controls)
// Gets the Delete link button
LinkButton DeleteButton = control as LinkButton;
if (DeleteButton != null && DeleteButton.Text == "Delete")
"return(confirm('Are you sure you want to delete this record?'))";
The above code will show you an alert message before deleting a row.