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 会自动将控件添加到包含服务器ControlCollection控件的。On 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.

适用于

另请参阅