ControlCollection 類別

定義

提供集合容器,讓 ASP.NET 伺服器控制項能夠保留其子控制項的清單。

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 字串屬性。 建立時,會將這兩個控制項新增至 ControlCollection 。 在每個 Add 控制項上呼叫 方法,以將它們新增至包含這個 版本的 類別 Control.CreateChildControls 。 屬性 ChildControlsCreated 設定為 true ,因此不會不必要地重新建立這些控制項。


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


備註

您可以透過 Control.Controls 屬性存取 類別的任何屬性和方法 ControlCollection 。 因為 類別 Control 是所有 ASP.NET 伺服器控制項的基類,所以所有伺服器控制項都會繼承這個屬性。

如需集合的詳細資訊,請參閱 集合和資料結構

建構函式

ControlCollection(Control)

初始化指定父伺服器控制項之 ControlCollection 類別的新執行個體。

屬性

Count

取得指定 ASP.NET 伺服器控制項的 ControlCollection 物件中的伺服器控制項數目。

IsReadOnly

取得值,這個值表示 ControlCollection 物件是否唯讀。

IsSynchronized

取得值,該值指出是否同步處理 ControlCollection 物件。

Item[Int32]

取得在 ControlCollection 物件中指定索引位置之伺服器控制項的參考。

Owner

取得 ControlCollection 物件所屬的 ASP.NET 伺服器控制項。

SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。

方法

Add(Control)

將指定的 Control 物件加入至集合。

AddAt(Int32, Control)

在指定索引位置將指定的 Control 物件加入集合。

Clear()

從目前伺服器控制項的 ControlCollection 物件中移除所有控制項。

Contains(Control)

判斷指定伺服器控制項是否在伺服器控制項的 ControlCollection 物件之中。

CopyTo(Array, Int32)

ControlCollection 中的指定索引位置開始,複製儲存於 Array 物件的子控制項至 Array 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

擷取可逐一查看 ControlCollection 物件的列舉值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Control)

擷取集合中指定 Control 物件的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(Control)

從父伺服器控制項的 ControlCollection 物件移除指定伺服器控制項。

RemoveAt(Int32)

在指定索引位置,從 ControlCollection 物件移除子控制項。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱