ControlCollection 類別

定義

提供集合容器,讓 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
繼承
ControlCollection
衍生
實作

範例

下列程式碼範例會覆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. 建立時,會將兩個控制項加入至ControlCollectionWhen 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 類別的新執行個體。Initializes a new instance of the ControlCollection class for the specified parent server control.

屬性

Count

取得指定 ASP.NET 伺服器控制項的 ControlCollection 物件中的伺服器控制項數目。Gets the number of server controls in the ControlCollection object for the specified ASP.NET server control.

IsReadOnly

取得值,這個值表示 ControlCollection 物件是否唯讀。Gets a value indicating whether the ControlCollection object is read-only.

IsSynchronized

取得值,指出 ControlCollection 物件是否已同步化。Gets a value indicating whether the ControlCollection object is synchronized.

Item[Int32]

取得在 ControlCollection 物件中指定索引位置之伺服器控制項的參考。Gets a reference to the server control at the specified index location in the ControlCollection object.

Owner

取得 ControlCollection 物件所屬的 ASP.NET 伺服器控制項。Gets the ASP.NET server control to which the ControlCollection object belongs.

SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。Gets an object that can be used to synchronize access to the collection of controls.

方法

Add(Control)

將指定的 Control 物件加入至集合。Adds the specified Control object to the collection.

AddAt(Int32, Control)

在指定索引位置將指定的 Control 物件加入集合。Adds the specified Control object to the collection at the specified index location.

Clear()

從目前伺服器控制項的 ControlCollection 物件中移除所有控制項。Removes all controls from the current server control's ControlCollection object.

Contains(Control)

判斷指定伺服器控制項是否在伺服器控制項的 ControlCollection 物件之中。Determines whether the specified server control is in the parent server control's ControlCollection object.

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)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

擷取可以逐一查看 ControlCollection 物件的列舉值。Retrieves an enumerator that can iterate through the ControlCollection object.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(Control)

擷取集合中指定 Control 物件的索引。Retrieves the index of a specified Control object in the collection.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Remove(Control)

從父伺服器控制項的 ControlCollection 物件移除指定伺服器控制項。Removes the specified server control from the parent server control's ControlCollection object.

RemoveAt(Int32)

在指定索引位置,從 ControlCollection 物件移除子控制項。Removes a child control, at the specified index location, from the ControlCollection object.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱