I’m maintaining an app at the moment that uses quite a few nested repeaters, and found that headers were being output when there was no data present. It was found that the header was being written in the ItemTemplate of an ‘outer’ repeater, rather than as the HeaderTemplate of the ‘inner’ repeater. The next problem was how to reference the outer repeater from the ‘inner’ HeaderTemplate…
The following will bind to a field called HeaderDescription.
<%# DataBinder.Eval(Container.Parent.Parent, “DataItem.HeaderDescription”) %>
The parent of the inner item is it’s repeater, so you have to go to it’s parent to get the right RepeaterItem. Why don’t you just do the following you ask?
<%# DataBinder.Eval(Container.Parent.Parent.DataItem, “HeaderDescription”) %>
..’cos it doesn’t work – The Eval method expects a ‘Control’ as its first parameter. There’s other ways to do this server-side, but the first option is probably the easiest.
To complete the picture and only show when there’s data you can add the following to the ‘inner’ repeater declaration
OnItemDataBound=”ItemDataBound” Visible=”false”
then..
protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
if (!e.Item.Parent.Visible)
e.Item.Parent.Visible = true;
}
}
This will ensure that you’ll only show if you’ve bound a ‘data’ item (remember you’re doing binding in the HeaderTemplate too). You could also hook similar things into other events, but it’s generally more convenient to put these things into events that relate to the actual control (pre_render’s probably another good candidate as it will only get called once and you can check the count in the DataSource).