Control.Controls Control.Controls Control.Controls Control.Controls Property

定義

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

public:
 virtual property System::Web::UI::ControlCollection ^ Controls { System::Web::UI::ControlCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
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.

範例

下列範例示範如何透過其ControlCollection Controls屬性,將子控制項加入至伺服器控制項的物件。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.

屬性可讓您以程式設計方式存取任何伺服器ControlCollection控制項的類別實例。 ControlsThe 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.

適用於

另請參閱