Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

I know this is an old question, but I had the same problem, and found the answer I think Bob's looking for.

The solution is to use the ObjectDataSource Updating event on the Web Form. The Updating event includes the ObjectDataSourceMethodEventArgs object as a parameter. The ObjectDataSourceMethodEventArgs class includes a propery named "InputParameters", and you can use that to access the data object and modify the contents before the update occurs. You need to convert the InputParameters object to an OrderedDictionary type first (full namespace is System.Collections.Specialized.OrderedDictionary)

It looks something like this:

protected void myObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
    OrderedDictionary parameters = (OrderedDictionary)e.InputParameters;
    MyDataObject updatedData = (MyDataObject)parameters[0];
    DropDownList myDropDown = (DropDownList)FormView1.FindControl("myDropDown")
    updatedData.SomeDataValue = myDropDown.SelectedValue;
}

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top