This topic has not yet been rated - Rate this topic

SPControlTemplateManager Class

Provides a method for retrieving one of the rendering template controls that are defined in one of the ascx files in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\CONTROLTEMPLATES\.

System.Object
  System.Web.UI.Control
    Microsoft.SharePoint.WebControls.SPControlTemplateManager

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class SPControlTemplateManager : Control, 
	INamingContainer
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
SPControlTemplateManager
Description 

The Microsoft.SharePoint.WebControls.SPControlTemplateManager class inherits from the System.Web.UI.INamingContainer marker interface and the System.Web.UI.Control which is the base class for ASP.NET server controls. SPControlTemplateManager provides three helpful methods when working with the user controls located at:

12\TEMPLATES\CONTROLTEMPLATES\

GetTemplateCollection – A helper method that returns a HashTable using the LoadControlTemplate method to build a collection of available templates

LoadControlTemplate – A helper method called by GetTemplateCollection in order to load the control template into the collection

GetTemplateByName – The most exposed method in the class. GetTemplateByName returns an ITemplate object which will index the GetTemplateCollection collection using a name string parameter for the appropriate template.

Usage Scenario

There is heavy representation of the SPControlTemplateManager class internally in order to build ITemplate objects with the GetTemplateByName method. Within custom development there are two primary uses, assignment of rendering controls in for custom field types (BaseFieldControl class) and for building new TemplateContainer objects.

In the below, I am demonstrating both such uses.

Firstly, I am using GetTemplateByName in order to hydrate a new ITemplate to assign to the TemplateContainer.Template property which is subsequently added to the current instance control collection.

Secondly, I am demonstrating for a class which is deriving from the BaseFieldControl class how the ControlTemplate property can be overriden and its value assigned by using the GetTemplateByName method.

C# Code Example

public class ExampleWebPart : WebPart
{
private TemplateContainer templateContainer;
protected override void CreateChildControls()
{
templateContainer = new TemplateContainer();
templateContainer.Template = SPControlTemplateManager.GetTemplateByName("ID");
Controls.Add(templateContainer);
}

public class ExampleFieldType : BaseFieldControl
{
protected override ITemplate ControlTemplate
{
get
{
if (Equals(ControlMode, SPControlMode.Display))
{
return SPControlTemplateManager.GetTemplateByName("MyFieldControl"); 
}
else
{
return base.ControlTemplate;
}
}


}

Visual Basic .NET Code Example 

Public Class ExampleWebPart
Inherits WebPart
Private templateContainer As TemplateContainer

Protected Overloads Overrides Sub CreateChildControls()
templateContainer = New TemplateContainer()
templateContainer.Template = SPControlTemplateManager.GetTemplateByName("ID")
Controls.Add(templateContainer)
End Sub
End Class


Public Class ExampleFieldType
Inherits BaseFieldControl
Protected Overloads Overrides ReadOnly Property ControlTemplate() As ITemplate
Get
If Equals(ControlMode, SPControlMode.Display) Then
Return SPControlTemplateManager.GetTemplateByName("MyFieldControl")
Else

Return MyBase.ControlTemplate

End If
End Get
End Property 
End Class

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com