4

In my Asp.net web page, I've got a GridView control that is data bound to an ObjectDataSource. The user can edit row directly in the GridView. There are times when the update fails validation. When this happens, I would like the row that was being updated to remain in the edit mode.

In the event handler for onUpdating, the event args object has a cancel property. But I need to check to see if the update failed in the onUpdated event handler, and it doesn't have e.Cancel property.

So I need to know how to get a GridView row to remain in edit mode if the update fails.

epotter
  • 7,631
  • 7
  • 63
  • 88
  • What do you mean with "update fails validation"? Are you using some kind of database validation(f.e. a Stored-Procedure with return values) or the ASP.NET validation(then use ASP.NET Validators as @Ovais suggested). – Tim Schmelter Jul 07 '11 at 15:47
  • The data access layer validates the data before it persists it to the database. So when I say that the update fails validation, I mean that the data access layer flagged the data as invalid. – epotter Jul 07 '11 at 16:31

3 Answers3

4

Very simply, you can keep the edit mode e.KeepInEditMode = true;

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
    e.KeepInEditMode = true;
}
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • This seems to work, you just have to variable that indicates whether or not to leave the row in edit mode. – epotter Jul 07 '11 at 16:28
0

One way to solve this problem is that you use Validation controls, which will restrict the user to send request if validation does not passes.

But to keep gridview in update mode, you must mantain its edit index property, because when gridview is not in edit mode usually its edit index is set to -ve value, but if it is in edit mode, gridview edit index is set to some positive integer value.

You could refer this link too: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdatedeventargs.keepineditmode.aspx

Ovais Khatri
  • 3,201
  • 16
  • 14
  • The validation is complicated and requires several database calls. It ends up being more efficient to do it server side than to try and validated in client side. – epotter Jul 07 '11 at 16:29
0

save the EditIndex value in a variable. Cancel the gridview update bu GridView1.EditIndex=-1; and then to keep the Gridview in edit mode you can again set the EditIndex value with the previously saved index value.

Saanch
  • 1,814
  • 1
  • 24
  • 38