0 out of 1 rated this helpful - Rate this topic

CompiledTemplateBuilder Class

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

An ITemplate implementation that is called from the generated page class code. This class cannot be inherited.

System.Object
  System.Web.UI.CompiledTemplateBuilder

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public sealed class CompiledTemplateBuilder : ITemplate

The CompiledTemplateBuilder type exposes the following members.

  Name Description
Public method CompiledTemplateBuilder Infrastructure. Initializes a new instance of the CompiledTemplateBuilder class.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InstantiateIn Infrastructure. Populates the Control object with the child controls contained in the template.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

The CompiledTemplateBuilder class is used by ASP.NET to create a template for a templated control from the generated class code. It is not meant to be used directly in your code.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

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.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Shouldn't be just for internal use.

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.