ASP.Net ITemplate - ways of declaring

1.7k Views Asked by At

when we want to define a Template in our user controls we declare a field like this in our user controls

public ITemplate MyTemplate { get; set; }

so that the user defined templates contents will be represented in MyTemplate, and you can use it.

and there are ways to customize the templates, for example

[TemplateInstanceAttribute(TemplateInstance.Single)]
public ITemplate MyTemplate { get; set; }

the above example will enable defines single instance Templates(http://www.nikhilk.net/SingleInstanceTemplates.aspx).

i accidentally came across single instance templates and blown away by the power of it.

my question is what are all the things possible with ITemplates?? how do we define(use) them (more specifically thru annotations). is there any good documentation available for ITemplates?? (please dont point to msdn)

1

There are 1 best solutions below

3
On

It does look like you're declaring the template properly. To actually populate the contents with your own template, you declare it in your markup. Eg:

<MyControl runat="server" ...>
  <MyTemplate>
   ... any standard ASP.NET controls in here
   <asp:Label runat="server" ID="lblName"/>
  </MyTemplate>
</MyControl>

  public void InstantiateIn(Control container) {
      var lblName = container.FindControl("lblName") as Label;
      lblName.Text = "Blah";// set from your data layer or otherwise
      Button b = new Button();
      b.ID = "B";
      container.Controls.Add(b);
  }

Is this what you were looking for?