jQuery and ASP.NET

December 27, 2009

How to cancel Update and Delete Operations in a GridView




I was recently asked a simple way to cancel update and delete operation in a GridView. It is quite simple to do so as shown here:

C#

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.Cancel = true;
}

void gv_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// Check if there is atleast one row left in the GridView
// before deleting
if (gv.Rows.Count <= 1)
{
e.Cancel = true;
}
}

VB.NET

Protected Sub gv_RowUpdating(ByVal sender As Object, _
ByVal e As GridViewUpdateEventArgs)
e.Cancel = True
End Sub

Private Sub
gv_RowDeleting(ByVal sender As Object, _
ByVal e As GridViewDeleteEventArgs)
' Check if there is atleast one row left in the GridView
' before deleting
If gv.Rows.Count <= 1 Then
e.Cancel = True
End If
End Sub
You can also add your own conditions before cancelling these events.

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "How to cancel Update and Delete Operations in a GridView"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions