CompiledTemplateBuilder Class
An ITemplate implementation that is called from the generated page class code. This class cannot be inherited.
Assembly: System.Web (in System.Web.dll)
The CompiledTemplateBuilder type exposes the following members.
| Name | Description | |
|---|---|---|
|
CompiledTemplateBuilder | Infrastructure. Initializes a new instance of the CompiledTemplateBuilder class. |
| Name | Description | |
|---|---|---|
|
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
|
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
|
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
|
GetType | Gets the Type of the current instance. (Inherited from Object.) |
|
InstantiateIn | Infrastructure. Populates the Control object with the child controls contained in the template. |
|
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
|
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
I'm not sure why this article says this class is not meant to be used from your code. I find it very useful when dynamically loading a custom templated user control that has other child controls. For example:
protected void Page_Init(object sender, EventArgs e)
{
var uc = (MyUserControl)this.LoadControl("MyUserControl.ascx");
uc.MyTemplate = new CompiledTemplateBuilder(
c =>
{
// add whatever controls you want
c.Controls.Add(new Literal { Text = "testing<br>" });
c.Controls.Add(new TextBox());
});
this.phSomePlaceHolder.Controls.Add(uc);
}
In this case, MyUserControl is a simple templated control without any custom naming container. It just has a single placeholder that the template is instantiated in.
Since the docs for ITemplate state "This interface is used by custom server controls, but never implemented by them. ASP.NET always implements it." - then this CompiledTemplateBuilder control should be the recommended implementation, rather than being discouraged from use.
There is some older documentation which talks about creating your own template class here:
http://msdn.microsoft.com/en-us/library/y0h809ak(v=vs.71).aspx
I find this to be unnecessary and confusing most of the time. Using CompiledTemplateBuilder as shown above is much easier.