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.

适用于

另请参阅