tags:

views:

120

answers:

0

I have a custom Web/Server Control that utilizes templates (ITemplate). Some of these templates repeat based on the provided data.

In order to allow for DataBinding/etc I need to "pre-generate" all of the defined templates via code very much like the following (this has been simplified, so keep the laughter to a minimum =):

public class TemplateContainer : Control, IDataItemContainer { ... }
TemplateContainer oTemplateContainer;
ITemplate oTemplate;
int iDisplayIndex = 0;
...

oTemplate = GetTemplate(enumTemplates.Header);
oTemplateContainer = new TemplateContainer(oDataItem, iDateItemIndex, iDisplayIndex);
oTemplate.InstantiateIn(oTemplateContainer);
Controls.AddAt(iDisplayIndex, oTemplateContainer);
iDisplayIndex++;

oTemplate = GetTemplate(enumTemplates.Detail);
oTemplateContainer = new TemplateContainer(oDataItem, iDateItemIndex, iDisplayIndex);
oTemplate.InstantiateIn(oTemplateContainer);
Controls.AddAt(iDisplayIndex, oTemplateContainer);
iDisplayIndex++;

oTemplate = GetTemplate(enumTemplates.Footer);
oTemplateContainer = new TemplateContainer(oDataItem, iDateItemIndex, iDisplayIndex);
oTemplate.InstantiateIn(oTemplateContainer);
Controls.AddAt(iDisplayIndex, oTemplateContainer);
iDisplayIndex++;

This code is run before the call to CreateChildControls is made so that any child controls present within the ITemplates are created/etc.

Based on my research, ITemplate.InstantiateIn basically just calls TemplateContainer.Controls.Add (see: http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.instantiatein.aspx). Is this correct?

If the above is indeed accurate, then what I need to know is what happens within TemplateContainer.Controls.Add?

The reason I ask is because I would like to be able to re-use these "pre-generated" controls rather then my current approach of:

1) "pre-generate" controls (which populates required underlying data structures)

2) Controls.Clear

3) Process data/calculate number of real controls needed

4) Generate real controls

As this has to be less efficient then being able to re-use the "pre-generated" controls. BUT I can only re-use the "pre-generated" controls if they are left in an unprocessed state (that is, TemplateContainer.Controls.Add doesn't result in any sort of "external" processing). I know that DataBinding and whatnot happen later in the process, but what does happen at Controls.Add?

Muchos Danke!

Cn