Continuing my ASP.NET GridView Tips and Tricks series, this post shows how to cancel the Update and Delete operation in an ASP.NET GridView.
To cancel an update and delete operation, we will make use of the RowUpdating and RowDeleting events. The events are as described below:
RowUpdating - Occurs when a row's Update button is clicked, but before the GridView control updates the row.
RowDeleting – Occurs when a row's Delete button is clicked, but before the GridView control deletes the row.
Here’s the code to cancel update and delete in a GridView:
// Cancel update operation
protected void Grid1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Check for a condition here and cancel update
e.Cancel = true;
}
// Cancel Delete operation
void Grid1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// Check for a condition here and cancel the delete
// Also make sure there's atleast one row left in the GridView
if (Grid1.Rows.Count <= 1)
{
e.Cancel = true;
}
}
Similarly, you may also want to read Display the details of the GridView Row being Deleted and Cancel the delete operation
Tweet
No comments:
Post a Comment