Control.CreateControlCollection 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立一個新 ControlCollection 物件來存放伺服器控制項的子控制項(字面控制項與伺服器控制項)。
protected:
virtual System::Web::UI::ControlCollection ^ CreateControlCollection();
protected virtual System.Web.UI.ControlCollection CreateControlCollection();
abstract member CreateControlCollection : unit -> System.Web.UI.ControlCollection
override this.CreateControlCollection : unit -> System.Web.UI.ControlCollection
Protected Overridable Function CreateControlCollection () As ControlCollection
傳回
一個 ControlCollection 物件,用來包含目前伺服器控制項的子伺服器控制項。
範例
以下程式碼範例覆寫了 CreateControlCollection 建立類別實例的方法,該實例 CustomControlCollection 會繼承自該 ControlCollection 類別。
// Override the CreateControlCollection method to
// write to the Trace object when tracing is enabled
// for the page or application in which this control
// is included.
protected override ControlCollection CreateControlCollection()
{
return new CustomControlCollection(this);
}
' Override the CreateControlCollection method to
' write to the Trace object when tracing is enabled
' for the page or application in which this control
' is included.
Protected Overrides Function CreateControlCollection() As ControlCollection
Return New CustomControlCollection(Me)
End Function
以下程式碼範例在自訂伺服器控制覆蓋CreateControlCollection中使用此CreateChildControls方法。 新集合被建立,然後填充兩個子控制項, firstControl 以及 secondControl。
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 類別衍生的集合物件,則在自訂伺服器控制中覆寫此方法。 接著你可以在這個方法的覆寫中實例化該集合類別。