Control.Controls Property


获取 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

Property Value


指定服务器控件的子控件的集合。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.

Notes to Inheritors

重写 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.

Applies to

See also