ASP.Net ITemplate - ways of declaring

一曲冷凌霜 提交于 2019-12-01 05:34:08

问题


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:


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?



来源:https://stackoverflow.com/questions/2610625/asp-net-itemplate-ways-of-declaring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!