I have several pages where I use a FormView control to display an object. The object is composed of several simple properties (strings, etc) and several collections. The collections are displayed using a Repeater control
I want to do a single query to pull the object from the database. However, the design of the web controls is that each is bound to it's own ObjectDataSource which means each Repeater control want to do it's own select.
The following works around this. There is no gaurantee that future versions of ASP.NET will work this way because it doesn't promise this ordering. But the event ordering does work under .NET 2.0.
I bind my object to the FormView. In the FormView I set OnSelected and have:
ReportTemplateItem tmp;
protected void ReportTemplateItem_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
ReportTemplateItem rti = (ReportTemplateItem)e.ReturnValue;
tmp = rti;
}
Then for the Repeater control(s), I do not set DataSourceID but I do set OnDataBinding. In OnDataBinding I have:
protected void DataSourceBinding(object sender, EventArgs e)
{
Repeater dsRows = (Repeater)sender;
dsRows.DataSource = tmp.Datasources;
}
Since both happen within the same request, the tmp variable is good between calls. And the select on the FormView is called before the binding on the Repeater inside it. So the FormView causes the object to be loaded from the database and the Repeater gets a collection property from the object.
Do you find this useful? If so please check out Windward Reports.
