Export (0) Print
Expand All
0 out of 4 rated this helpful - Rate this topic

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 : ICollection, IEnumerable
public class ControlCollection implements ICollection, IEnumerable
public class ControlCollection implements ICollection, IEnumerable
Not applicable.

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 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;
}


System.Object
  System.Web.UI.ControlCollection
     Derived Classes
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.