How's that for a mouthful? I had two databound controls on an asp.net page, each being loaded on the page load event. After the initial project review, the requirements were changed and they wanted me to place one of the controls within the other so that it was repeated with each iteration. I thought the solution was pretty interesting so I decided to post it in case it might help anyone.
The trick my friends, is to load the data once, then bind the "child" repeater every time the "parent" repeater gets an item databound. The
OnItemDataBound event is perfect for this. Simply scope your data source at the class level so that you can reference it when doing the databinding and you're set.**
....
....
Remember to only try to bind if the ItemType is either an Item or AlternatingItem because this event will get fired for all templates, including headers and footers.
private void Maps_DataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater cols = e.Item.FindControl("mColorKey") as Repeater;
cols.DataSource = this.colorList;
cols.DataBind();
}
}
I know this isn't exactly ground breaking ... but sometimes you get so caught up in the world of patterns, practices, application blocks, new technology, new languages ... that it gets easy to forget, and even take for granted the simple little techniques that make life easier in your day to day programming life.