Control.HasControls Control.HasControls Control.HasControls Control.HasControls Method

定義

判斷伺服器控制項是否包含任何子控制項。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()在變更中繼資料屬性(例如)時EditorBrowsableAttribute,才應該覆寫方法。The HasControls() method should be overridden only to change metadata attributes such as EditorBrowsableAttribute. 如需使用屬性的詳細資訊,請參閱屬性For more information about using attributes, see Attributes.

適用於

另請參閱