Cancel Update and Delete in ASP.NET GridView

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






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: