This documentation is archived and is not being maintained.

ControlCollection Class

Provides a collection container that enables ASP.NET server controls to maintain a list of their child controls.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Class ControlCollection _
	Implements ICollection, IEnumerable

The ControlCollection type exposes the following members.

Public methodControlCollectionInitializes a new instance of the ControlCollection class for the specified parent server control.

Public propertyCountGets the number of server controls in the ControlCollection object for the specified ASP.NET server control.
Public propertyIsReadOnlyGets a value indicating whether the ControlCollection object is read-only.
Public propertyIsSynchronizedGets a value indicating whether the ControlCollection object is synchronized.
Public propertyItemGets a reference to the server control at the specified index location in the ControlCollection object.
Protected propertyOwnerGets the ASP.NET server control to which the ControlCollection object belongs.
Public propertySyncRootGets an object that can be used to synchronize access to the collection of controls.

Public methodAddAdds the specified Control object to the collection.
Public methodAddAtAdds the specified Control object to the collection at the specified index location.
Public methodClearRemoves all controls from the current server control's ControlCollection object.
Public methodContainsDetermines whether the specified server control is in the parent server control's ControlCollection object.
Public methodCopyToCopies the child controls stored in the ControlCollection object to an System.Array object, beginning at the specified index location in the System.Array.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorRetrieves an enumerator that can iterate through the ControlCollection object.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfRetrieves the index of a specified Control object in the collection.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the specified server control from the parent server control's ControlCollection object.
Public methodRemoveAtRemoves a child control, at the specified index location, from the ControlCollection object.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast(Of TResult)Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType(Of TResult)Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

You can access any of the properties and methods of the ControlCollection class through the Control.Controls property. Since the Control class is the base class for all ASP.NET server controls, all server controls inherit this property.

For more information about collections, see Collections and Data Structures.

The following code example overrides the Control.CreateChildControls method to create two controls, both instances of the custom class named ChildControl, which has a string property named Message. When created, the two controls are added to the ControlCollection. The Add method is called on each of these controls to add them to the class that contains this version of Control.CreateChildControls. The ChildControlsCreated property is set to true so that these controls will not be created again unnecessarily.

Protected Overrides Sub CreateChildControls()
   ' Creates a new ControlCollection. 

   ' Create child controls.
   Dim firstControl As New ChildControl()
   firstControl.Message = "FirstChildControl"

   Dim secondControl As New ChildControl()
   secondControl.Message = "SecondChildControl"


   ' Prevent child controls from being created again.
   ChildControlsCreated = True
End Sub 'CreateChildControls

.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.