Sharing ObjectDataSource From Codebehind

A04C0035

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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • GridView and Updating A Row ManuallyGridView and Updating A Row Manually A couple of days ago I mentioned a project that I’ve been working on that is a bit out of the ordinary as far as GridViews go.  One of the issues I’ve had is that the edit template […]
  • Conditional Footnotes On GridViewConditional Footnotes On GridView A couple of days ago I ran into a requirement to conditionally display footnote information at the bottom of a GridView display.  We were already conditionally displaying icons in the […]
  • Access a control by ID From Within a Databound ControlAccess a control by ID From Within a Databound Control Databound controls are at once very easy and very frustrating.  If you just need to do some simple databinding that gets a list of items on the screen and you need the ability to […]
  • GridViews – Multiple Rows Per RecordGridViews – Multiple Rows Per Record I don't know about you, but I've had several occasions where I've needed to use the simplicity of the GridView control in ASP.NET.  DataBinding, Paging, Sorting, etc.  But I've also […]
  • FormView Edit and AddFormView Edit and Add The FormView generally makes editing a record at a time pretty easy.  However, one of the biggest problems I've seen with this control is that there is no way of telling the FormView […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback