Export (0) Print
Expand All

ControlCollection Class

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

For a list of all members of this type, see ControlCollection Members.

System.Object
   System.Web.UI.ControlCollection
      System.Web.UI.EmptyControlCollection

[Visual Basic]
Public Class ControlCollection
   Implements ICollection, IEnumerable
[C#]
public class ControlCollection : ICollection, IEnumerable
[C++]
public __gc class ControlCollection : public ICollection,
   IEnumerable
[JScript]
public class ControlCollection implements ICollection, IEnumerable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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 Grouping Data in Collections.

Example

[Visual Basic, C#, C++] The following example overrides the Control.CreateChildControls method to create two create two controls, instantiated versions of the custom class named ChildControl, which has a string property named Message. Once created, the two controls are added to the Add method is called on each of these controls to add them to the class that contains this version of CreateChildControls. The Control.ChildControlsCreated property is set to true so that these controls will not be created again unnecessarily.

[Visual Basic] 
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



[C#] 

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


[C++] 

protected:
   void CreateChildControls()
   {               
      // Creates a new ControlCollection. 
      this->CreateControlCollection();

      // Create child controls.
      ChildControl* firstControl = new ChildControl();
      firstControl->Message = S"FirstChildControl";

      ChildControl* secondControl = new ChildControl();
      secondControl->Message = S"SecondChildControl";

      Controls->Add(firstControl);
      Controls->Add(secondControl);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

ControlCollection Members | System.Web.UI Namespace | Grouping Data in Collections | Control | Page | Controls

Show:
© 2014 Microsoft