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.

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.

適用於

另請參閱