ControlCollection ControlCollection ControlCollection ControlCollection Class

定义

提供使 ASP.NET 服务器控件能够维护其子控件列表的集合容器。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
继承
ControlCollectionControlCollectionControlCollectionControlCollection
派生
实现

示例

下面的代码示例将重Control.CreateChildControls写方法,以创建两个控件,即名为ChildControl的自定义类的两个实例Message,该实例具有一个名为的字符串属性。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. 创建后,这两个控件将添加到ControlCollection中。When created, the two controls are added to the ControlCollection. 对每个控件调用Control.CreateChildControls方法,以将其添加到包含此版本的类。AddThe Add method is called on each of these controls to add them to the class that contains this version of Control.CreateChildControls. 将属性设置为true ,以便不会再次创建这些控件。 ChildControlsCreatedThe 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


注解

您可以ControlCollection Control.Controls通过属性访问类的任何属性和方法。You can access any of the properties and methods of the ControlCollection class through the Control.Controls property. Control由于类是所有 ASP.NET 服务器控件的基类,因此所有服务器控件都继承该属性。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.

构造函数

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

为指定的父服务器控件初始化 ControlCollection 类的新实例。Initializes a new instance of the ControlCollection class for the specified parent server control.

属性

Count Count Count Count

为指定的 ASP.NET 服务器控件获取 ControlCollection 对象中的服务器控件数。Gets the number of server controls in the ControlCollection object for the specified ASP.NET server control.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

获取一个值,该值指示 ControlCollection 对象是否为只读。Gets a value indicating whether the ControlCollection object is read-only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

获取一个值,该值指示 ControlCollection 对象是否已同步。Gets a value indicating whether the ControlCollection object is synchronized.

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

获取对 ControlCollection 对象中指定索引位置的服务器控件的引用。Gets a reference to the server control at the specified index location in the ControlCollection object.

Owner Owner Owner Owner

获取 ControlCollection 对象所属的 ASP.NET 服务器控件。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.

方法

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

将指定的 Control 对象添加到集合。Adds the specified Control object to the collection.

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

将指定的 Control 对象添加到集合中的指定索引位置。Adds the specified Control object to the collection at the specified index location.

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

从当前服务器控件的 ControlCollection 对象中移除所有控件。Removes all controls from the current server control's ControlCollection object.

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

确定指定的服务器控件是否在父服务器控件的 ControlCollection 对象中。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)

ControlCollection 中的指定索引位置开始,将 Array 对象中存储的子控件复制到 Array 对象。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()

检索可以循环访问 ControlCollection 对象的枚举数。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()

获取当前实例的 TypeGets the Type of the current instance.

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

检索集合中指定 Control 对象的索引。Retrieves the index of a specified Control object in the collection.

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

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

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

从父服务器控件的 ControlCollection 对象中移除指定服务器控件。Removes the specified server control from the parent server control's ControlCollection object.

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

ControlCollection 对象中移除指定索引位置处的子控件。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)

扩展方法

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

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

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

根据指定类型筛选 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)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅