I've been trying to extend a DataList to support an EmptyDataTemplate, but I can't figure out how to actually add the contents of the EmptyDataTemplate as a list item. I create the new list item, instantiate it in the empty data template, and add it to the controls collection but I can't figure out how to add the item I created to the list items (Items.Count is still zero). I wind up with a data list with a caption and a border but no content whatsoever inside. Am I even approaching this the right way?
So far, what I have is:
public class MyDataList : System.Web.UI.WebControls.DataList
{
private ITemplate emptyDataTemplate = null;
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate EmptyDataTemplate
{
get { return emptyDataTemplate; }
set { emptyDataTemplate = value; }
}
protected override void CreateControlHierarchy(bool useDataSource)
{
base.CreateControlHierarchy(useDataSource);
if (Items.Count == 0)
{
if (emptyDataTemplate != null)
{
DataListItem emptyDataItem = this.CreateItem(0, ListItemType.Item);
EmptyDataTemplate.InstantiateIn(emptyDataItem);
this.Controls.Add(emptyDataItem);
// OK, now what? Items.Count is still 0 and there is no Items.Add()
// emptyDataItem, when inspected on a breakpoint, seems OK (it contains
// the text/controls in my EmptyDataTemplate), it just doesn't get
// rendered.
}
}
}
}