ControlCollection.Count 属性

定义

为指定的 ASP.NET 服务器控件获取 ControlCollection 对象中的服务器控件数。Gets the number of server controls in the ControlCollection object for the specified ASP.NET server control.

public:
 property int Count { int get(); };
public:
 virtual property int Count { int get(); };
public int Count { get; }
public virtual int Count { get; }
member this.Count : int
Public ReadOnly Property Count As Integer
Public Overridable ReadOnly Property Count As Integer

属性值

ControlCollection 中的服务器控件数。The number of server controls in the ControlCollection.

实现

示例

下面的代码示例演示如何使用 Count 属性来循环访问控件的 ControlCollection 对象。The following code example demonstrates how to use the Count property to iterate through a control's ControlCollection object. 使用 Control.HasControls 属性将验证控件是否具有子控件。The use of the Control.HasControls property verifies that the control has child controls. 如果 Control.HasControls 返回 false,则剩余的代码不会运行。If Control.HasControls returns false, the remaining code does not run.

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

适用于

另请参阅