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



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: