Control.Controls 屬性


取得 ControlCollection 物件,表示 UI 階層架構中指定之伺服器控制項的子控制項。Gets a ControlCollection object that represents the child controls for a specified server control in the UI hierarchy.

 virtual property System::Web::UI::ControlCollection ^ Controls { System::Web::UI::ControlCollection ^ get(); };
public virtual System.Web.UI.ControlCollection Controls { get; }
member this.Controls : System.Web.UI.ControlCollection
Public Overridable ReadOnly Property Controls As ControlCollection


指定之伺服器控制項的子控制項集合。The collection of child controls for the specified server control.



下列範例示範如何透過其 Controls 屬性,將子控制項加入至伺服器控制項的 ControlCollection 物件。The following example demonstrates how to add child controls to a server control's ControlCollection object through its Controls property.

Controls.Add(new LiteralControl("<h3>Value: "));
Controls.Add(New LiteralControl("<h3>Value: "))


在 ASP.NET 網頁上,當控制項以宣告方式加入伺服器控制項的開頭和結束記號之間時,ASP.NET 會自動將控制項加入至包含伺服器控制項的 ControlCollectionOn an ASP.NET page, when controls are added declaratively between the opening and closing tags of a server control, ASP.NET automatically adds the controls to the containing server control's ControlCollection. 未在伺服器上處理的任何 HTML 標籤或文字字串都會被視為 LiteralControl 物件。Any HTML tags or text strings that are not processed on the server are treated as LiteralControl objects. 這些會加入至集合,如其他伺服器控制項。These are added to the collection like other server controls.

Controls 屬性可讓您以程式設計方式存取任何伺服器控制項的 ControlCollection 類別實例。The Controls property allows you programmatic access to the instance of the ControlCollection class for any server control. 您可以將控制項加入至集合、從集合中移除控制項,或是逐一查看集合中的伺服器控制項。You can add controls to the collection, remove controls from the collection, or iterate through the server controls in the collection.


覆寫 Controls 屬性時,請務必呼叫 EnsureChildControls() 方法。When overriding the Controls property, be sure to call the EnsureChildControls() method. 此外,如果您打算以程式設計方式將控制項加入控制項集合,請考慮在覆寫的 CreateChildControls() 方法中加入控制項。Furthermore, if you are planning to add controls to the controls collection programmatically, consider adding the controls in an overridden CreateChildControls() method.