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,其中包含字串屬性,名為MessageThe 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. 建立時,會新增兩個控制項ControlCollectionWhen created, the two controls are added to the ControlCollection. Add將它們加入至類別,其中包含此版本的這些控制項上呼叫方法Control.CreateChildControlsThe Add method is called on each of these controls to add them to the class that contains this version of Control.CreateChildControls. ChildControlsCreated屬性設定為true,讓這些控制項不會無謂地重新建立。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


備註

您可以存取的任何屬性和方法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

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。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)

Extension Methods

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)

將轉換IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱