ControlCollection ControlCollection ControlCollection ControlCollection Class

Definition

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

public ref class ControlCollection : System::Collections::ICollection
public class ControlCollection : System.Collections.ICollection
type ControlCollection = class
    interface ICollection
    interface IEnumerable
Public Class ControlCollection
Implements ICollection
Inheritance
ControlCollectionControlCollectionControlCollectionControlCollection
Derived
Implements

Examples

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 override void CreateChildControls()
{               
   // Creates a new ControlCollection. 
   this.CreateControlCollection();

   // Create child controls.
    ChildControl firstControl = new ChildControl();
   firstControl.Message = "FirstChildControl";

   ChildControl secondControl = new ChildControl();
   secondControl.Message = "SecondChildControl";
   
   Controls.Add(firstControl);
   Controls.Add(secondControl);

   // Prevent child controls from being created again.
   ChildControlsCreated = true;
}

Protected Overrides Sub CreateChildControls()
   ' Creates a new ControlCollection. 
   Me.CreateControlCollection()
   
   ' Create child controls.
   Dim firstControl As New ChildControl()
   firstControl.Message = "FirstChildControl"
   
   Dim secondControl As New ChildControl()
   secondControl.Message = "SecondChildControl"
   
   Controls.Add(firstControl)
   Controls.Add(secondControl)
   
   ' Prevent child controls from being created again.
   ChildControlsCreated = True
End Sub 'CreateChildControls


Remarks

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.

Constructors

ControlCollection(Control) ControlCollection(Control) ControlCollection(Control) ControlCollection(Control)

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

Properties

Count Count Count Count

Gets the number of server controls in the ControlCollection object for the specified ASP.NET server control.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

Gets a value indicating whether the ControlCollection object is read-only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

Gets a value indicating whether the ControlCollection object is synchronized.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets a reference to the server control at the specified index location in the ControlCollection object.

Owner Owner Owner Owner

Gets the ASP.NET server control to which the ControlCollection object belongs.

SyncRoot SyncRoot SyncRoot SyncRoot

Gets an object that can be used to synchronize access to the collection of controls.

Methods

Add(Control) Add(Control) Add(Control) Add(Control)

Adds the specified Control object to the collection.

AddAt(Int32, Control) AddAt(Int32, Control) AddAt(Int32, Control) AddAt(Int32, Control)

Adds the specified Control object to the collection at the specified index location.

Clear() Clear() Clear() Clear()

Removes all controls from the current server control's ControlCollection object.

Contains(Control) Contains(Control) Contains(Control) Contains(Control)

Determines whether the specified server control is in the parent server control's ControlCollection object.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

Copies the child controls stored in the ControlCollection object to an Array object, beginning at the specified index location in the Array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Retrieves an enumerator that can iterate through the ControlCollection object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(Control) IndexOf(Control) IndexOf(Control) IndexOf(Control)

Retrieves the index of a specified Control object in the collection.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(Control) Remove(Control) Remove(Control) Remove(Control)

Removes the specified server control from the parent server control's ControlCollection object.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

Removes a child control, at the specified index location, from the ControlCollection object.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See Also