views:

298

answers:

2

I can't seem to find any examples on how to implement ITemplates with multiple instances. All I need to do is build out a navigation that has a template for the content of each navigation item.

A: 

Look at documentation of creating a data-bound templated control.

Unfortunately, the best documentation I've found is from .NET 1.1: Developing a Templated Data-Bound Control.


Note from MSDN:

This TemplateInstanceAttribute class is optional. If a template property is not extended with a TemplateInstanceAttribute class, the default value, the Multiple field, is used.

So any ITemplate example that does not use the TemplateInstanceAttribute is using TemplateInstance.Multiple.

John Saunders
Though this will probably help I actually don't need or want my control to be databound.
Chris
I didn't suggest that your control should be databound, only that you should look at the documentation, as it was likely the only documentation of creating multiple instances of a single `ITemplate` parameter.
John Saunders
+1  A: 

If your control is not a databound control, you can solve the problem by something as follows. But, I haven't tested it.

public class FooItem : WebControl, INamingContainer
{

    protected override CreateChildControls()
    {
        Control placeHolder = this.FindControl(((Foo)this.Parent).ItemPlaceHolderId);
        if (placeHolder != null)
        {
            // Add some controls to the placeHolder.
        }
    }

}


public class Foo : WebControl, INamingContainer
{

    public ITemplate ItemTemplate { get; set; }

    public string ItemPlaceHolderId
    {
        get { ... }
        set { ... }
    }

    public FooItemCollection Items
    {
        get { ... }
    }

    protected override CreateChildControls()
    {
        foreach (FooItem item in this.Items)
        {
            this.ItemTemplate.InstantiateIn(item);
        }
    }

}
Mehdi Golchin