Sharing ObjectDataSource From Codebehind
Join the DZone community and get the full member experience.Join For Free
I ran into an interesting “problem” last week that wasn’t all that hard to figure out once I broke out the debugger. But, I thought it would be useful to share the results to save others the time.
The situation was that I had an Object Data Source in a FormView that I wanted to share with both a DataGrid in the FormView and a DataRepeater outside of the FormView. Since it was in the FormView, the DataRepeater couldn’t see it.
So, it seemed reasonable to me that if I could just trap the right event in the FormView I could assign the DataSource of the GridView to the DataSource of the Repeater, rebind the Repeater and we’d be on our way.
It wasn’t quite that simple.
The first thing I tried was to trap the Load event of the ObjectDataSource control and assign it to the Repeater. That didn’t work at all.
Next I decided to trap the DataBound event of the GridView and assign the DataSource of the GridView to the DataSource of the Repeater. But the DataSource object of the GridView was null.
What’s up with that?
After a bit of inspection, I noticed that the GridView also has a DataSourceObject property which has the DataSource object in it. But the Repeater only has a DataSource property.
Turns out, if you assign the GridView’s DataSourceObject property to the Repeater’s DataSource property and rebind, everything works.
Published at DZone with permission of Dave Bush, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.