Export (0) Print
Expand All

Control.HasControls Method

Determines if the server control contains any child controls.

[Visual Basic]
Public Overridable Function HasControls() As Boolean
[C#]
public virtual bool HasControls();
[C++]
public: virtual bool HasControls();
[JScript]
public function HasControls() : Boolean;

Return Value

true if the control contains other controls; otherwise, false.

Remarks

Since this method simply determines if any child controls exist, it can enhance performance by allowing you to avoid an unnecessary Controls.Count property call. Calls to the this property require a ControlCollection object to be instantiated. If there are no children, this object creation wastes server resources.

Example

The following example uses the HasControls method to determine if any controls exist before using the Control.Count property to iterate through a ControlCollection object.

[Visual Basic] 
If HasControls() Then
    Dim i As Integer
    For i = 0 To Controls.Count - 1
        Controls(i).RenderControl(writer)
    Next i
End If

[C#] 
if (HasControls()) {
                 for (int i=0; i < Controls.Count; i++) {
                     Controls[i].RenderControl(writer);
                 }
   }

[C++] 
if (HasControls()) {
   for (int i=0; i < Controls->Count; i++) {
      Controls->Item[i]->RenderControl(writer);
   }
}

[JScript] 
if (HasControls()) {
                 for (var i : int = 0; i < Controls.Count; i++) {
                     Controls[i].RenderControl(writer);
                 }
   }

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | Controls | ControlCollection

Show:
© 2014 Microsoft