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.

Related Post

One Response to “Sharing ObjectDataSource From Codebehind”

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail