Control.HasControls 方法

定义

确定服务器控件是否包含任何子控件。Determines if the server control contains any child controls.

public:
 virtual bool HasControls();
public virtual bool HasControls ();
abstract member HasControls : unit -> bool
override this.HasControls : unit -> bool
Public Overridable Function HasControls () As Boolean

返回

如果控件包含其他控件,则为 true;否则为 falsetrue if the control contains other controls; otherwise, false.

示例

下面的示例使用 HasControls 方法来确定在使用 Count 属性循环访问 ControlCollection 对象之前是否存在任何控件。The following example uses the HasControls method to determine if any controls exist before using the Count property to iterate through a ControlCollection object.

if (HasControls()) {
                 for (int i=0; i < Controls.Count; i++) {
                     Controls[i].RenderControl(writer);
                 }
   }
If HasControls() Then
    Dim i As Integer
    For i = 0 To Controls.Count - 1
        Controls(i).RenderControl(writer)
    Next i
End If

注解

由于此方法只是确定是否存在任何子控件,因此它可以通过允许避免不必要的 Count 属性调用来提高性能。Since this method simply determines if any child controls exist, it can enhance performance by allowing you to avoid an unnecessary Count property call. 对此属性的调用需要实例化 ControlCollection 的对象。Calls to this property require a ControlCollection object to be instantiated. 如果没有子级,则创建此对象会浪费服务器资源。If there are no children, this object creation wastes server resources.

继承者说明

只应重写 HasControls() 方法来更改元数据特性,如 EditorBrowsableAttributeThe HasControls() method should be overridden only to change metadata attributes such as EditorBrowsableAttribute. 有关使用特性的详细信息,请参阅特性For more information about using attributes, see Attributes.

适用于

另请参阅