views:

330

answers:

4

Hello,

I have a simple web form that has a several fields and a gridview on it. I also have a save and cancel button.

I would like a way to undo what was done to the data on the form when the user presses cancel. This is simple enough with the fields however since changes to the gridview happen in real time against the database I do not know how to get undo functionality.

I have thought of storing the changes to gridview in viewstate but I would rather not because of the extra space requirement.

I have also thought of a temporary table that would stored the changes then roll them back as needed.

Does anyone have an idea how to get undo functionality from the form?

A: 

One way would be to store changes to a table in another table together with a timestamp and a identifier for that application instance. If you want to undo changes since a specific time you just traverse the list backwards until that date for that identifier.

Tobias
+1  A: 

Keep all the data in a session object and write that to the database when you are ready. If you abstract your data layer you can use the ObjectDataSource that interacts with the session objects.

I'm currently using this method on a checkout system for an e-commerce site. I store the data in custom objects that mimic the database schema.

craigmoliver
+1  A: 

The simplest solution is to not commit the changes in the gridview to the database until the user clicks the "save" button.

If you do decide to use viewstate or some such to record changes that you will later undo, don't forget to take the same sorts of precautions re: update collisions that you would when making the initial changes.

Matt
A: 

Hmmm... load the data object(s) into session and bind your controls from the (MyObject)Session["MyObject"] objects. I believe you can hook into an ObjectDataSource to use the session... you can then override the Update events so that the changes are written to the session.

When the user clicks save, take the session objects and save them: MyObject obj = (MyObject)Session["MyObject"]; MyObject.Save()

It wouldn't give you multiple levels of undo... although I guess you could save multiple session objects if you really needed to.

Keith Williams