Over a million developers have joined DZone.

Sharing ObjectDataSource From Codebehind

·

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.

 

Topics:

Published at DZone with permission of Dave Bush, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}