SPCompositeControl Class
System.Object
System.Web.UI.Control
Microsoft.SharePoint.WebControls.SPCompositeControl
Microsoft.SharePoint.WebControls.DateTimeControl
Microsoft.SharePoint.WebControls.RecurrenceDataControl
System.Web.UI.Control
Microsoft.SharePoint.WebControls.SPCompositeControl
Microsoft.SharePoint.WebControls.DateTimeControl
Microsoft.SharePoint.WebControls.RecurrenceDataControl
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
SPCompositeControl
Description
The abstract Microsoft.SharePoint.WebControls.SPCompositeControl class inherits from the System.Web.UI.Control class for traditional ASP.NET server control support, as well as the System.Web.UI.INamingContainer interface as a marker interface in order to produce unique naming that targets the complete page scope. In order to function, the SPCompositeControl class relies on the INamingContainer interface to handle any of its child controls, since it acts as a container control.
Usage Scenario
The principal usage of SPCompositeControl is as a base class for inheritance for when working with native SharePoint controls that inherit from SPCompositeControl like Microsoft.SharePoint.WebControls.DateTimeControl as a cast type to get access to child control properties, in the context of a DateTimeControl this is how the date value is gathered, within a TextBox, rather than using the SelectedDate property directly on the control.
In the below, the MyClass class is inheriting from the System.Web.UI.WebControls.WebParts.WebPart class. A class level Control typed list collection is provided, which is used later for housing a created SPCompositeControl object. In the overridden CreateChildControls method, the FindControl method is used to create a new DateTimeControl facilitated by safely casting the resulting Control. The previously created DateTimeControl is then used to build a new SPCompositeControl by casting. The resulting SPCompositeObject Controls collection can be indexed following to expose child object properties. Lastly, the control is then added to the class level typed Control collection.
C# Code Example
public class MyClass : WebPart
{
readonly List<Control> collection = new List<Control>();
protected override void CreateChildControls()
{
DateTimeControl dateTime = FindControl("DateTimeControlID") as DateTimeControl;
SPCompositeControl compositeControl = (SPCompositeControl) dateTime;
collection.Add(compositeControl);
}
}
VB.NET Code Example
Public Class [MyClass]
Inherits WebPart
ReadOnly collection As New List(Of Control)()
Protected Overloads Overrides Sub CreateChildControls()
Dim dateTime As DateTimeControl = TryCast(FindControl("DateTimeControlID"), DateTimeControl)
Dim compositeControl As SPCompositeControl = DirectCast(dateTime, SPCompositeControl)
collection.Add(compositeControl)
End Sub
End Class
The abstract Microsoft.SharePoint.WebControls.SPCompositeControl class inherits from the System.Web.UI.Control class for traditional ASP.NET server control support, as well as the System.Web.UI.INamingContainer interface as a marker interface in order to produce unique naming that targets the complete page scope. In order to function, the SPCompositeControl class relies on the INamingContainer interface to handle any of its child controls, since it acts as a container control.
Usage Scenario
The principal usage of SPCompositeControl is as a base class for inheritance for when working with native SharePoint controls that inherit from SPCompositeControl like Microsoft.SharePoint.WebControls.DateTimeControl as a cast type to get access to child control properties, in the context of a DateTimeControl this is how the date value is gathered, within a TextBox, rather than using the SelectedDate property directly on the control.
In the below, the MyClass class is inheriting from the System.Web.UI.WebControls.WebParts.WebPart class. A class level Control typed list collection is provided, which is used later for housing a created SPCompositeControl object. In the overridden CreateChildControls method, the FindControl method is used to create a new DateTimeControl facilitated by safely casting the resulting Control. The previously created DateTimeControl is then used to build a new SPCompositeControl by casting. The resulting SPCompositeObject Controls collection can be indexed following to expose child object properties. Lastly, the control is then added to the class level typed Control collection.
C# Code Example
public class MyClass : WebPart
{
readonly List<Control> collection = new List<Control>();
protected override void CreateChildControls()
{
DateTimeControl dateTime = FindControl("DateTimeControlID") as DateTimeControl;
SPCompositeControl compositeControl = (SPCompositeControl) dateTime;
collection.Add(compositeControl);
}
}
VB.NET Code Example
Public Class [MyClass]
Inherits WebPart
ReadOnly collection As New List(Of Control)()
Protected Overloads Overrides Sub CreateChildControls()
Dim dateTime As DateTimeControl = TryCast(FindControl("DateTimeControlID"), DateTimeControl)
Dim compositeControl As SPCompositeControl = DirectCast(dateTime, SPCompositeControl)
collection.Add(compositeControl)
End Sub
End Class
Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com
- 6/3/2010
- Adam Buenz - MVP