How do you manually modify a bound object with a FormView?

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.

Asked on April 7, 2016 in .NET,   ASP.NET,   C#.
Add Comment
1 Answer(s)

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;
Answered on April 7, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.