views:

533

answers:

3

I am using a FormView with an ObjectDataSource. When the save button is clicked, I would like to modify the data bound object before it gets set to the ObjectDataSources Update method.

I tried the FormView's Updating event as well as the Object Data Source's Updating event but I can't figure out how to access the data bound object. FormView.DataItem is null in those events.

Or in other words, I would like to intercept and modify the DataItem before it gets passed to the ObjectDataSource UpdateMethod.

To give a little more detail on why I want to do this, there are some values on the form which can't be databound with the build in functionality. One of the controls is the checkbox list. I am using the DataBinding event to populate the checks, but now I also need a way to update my object to reflect the form values. There are also other controls with similar situations.

A: 

Since you are in the Updating event, FormView.DataItem is null because data binding has not yet occurred. You have to access the data via the form control containing your data of interest.

Try applying your data modification during the OnDataBinding event of the relevant control.

Robert Harvey
I don't want to access the data that is being posted, I want to access the actual object which will be sent to the ObjectDataSource's Update method so that I can set some field on it.
Bob
By then, it is too late. You have to capture it before data binding has occurred. Unless you want to just read it back from the database or business object after the fact, perform your modification, and then write it back.
Robert Harvey
A: 

Why don't you just write your own business object (aka ObjectDataSource), and wrap the original ObjectDataSource object? You can then intercept anything you want, and modify it enroute to the original ObjectDataSource object's Save method.

Robert Harvey
Is there an example of this? I am not sure it is what I am trying to accomplish
Bob
A: 

DataItem is only available when DataBinding.
Data is then bound to controls inside your FormView.
Use myFormView.FindControl(string id) to access bound values before Updating.

If two-way databinding won't work for you, you should instanciate your object, populate manually the properties and then update or commit the changes.

Maxime