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.

給繼承者的注意事項

只有在變更中繼資料屬性(例如 EditorBrowsableAttribute)時,才應該覆寫 HasControls() 方法。The HasControls() method should be overridden only to change metadata attributes such as EditorBrowsableAttribute. 如需使用屬性的詳細資訊,請參閱屬性For more information about using attributes, see Attributes.

適用於

另請參閱