ControlCollection.GetEnumerator Method ()


Retrieves an enumerator that can iterate through the ControlCollection object.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Overridable Function GetEnumerator As IEnumerator

Return Value

Type: System.Collections.IEnumerator

The enumerator to iterate through the collection.

The following code example creates a method that enumerates through the ControlCollection collection of a Button control, myButton. When the enumerator is created, the IsSynchronized property is checked to see if the operation is thread safe, and if it is not, the SyncRoot property is used to obtain an object to make the operation thread safe. When the enumeration is completed, the value of the IsReadOnly property is written as the Text property of a Label control on the containing page.

' Create a method that enuberates through a 
' button's ControlCollection in a thread-safe manner.  
Public Sub ListControlCollection(sender As Object, e As EventArgs)
   Dim myEnumerator As IEnumerator = myButton.Controls.GetEnumerator()

   ' Check the IsSynchronized property. If False,
   ' use the SyncRoot method to get an object that 
   ' allows the enumeration of all controls to be 
   ' thread safe.
   If myButton.Controls.IsSynchronized = False Then
     SyncLock myButton.Controls.SyncRoot
       While (myEnumerator.MoveNext())

       Dim myObject As Object  = myEnumerator.Current

         Dim childControl As LiteralControl = CType(myEnumerator.Current, LiteralControl)
         Response.Write("<b><br /> This is the  text of the child Control  </b>: " & _
       End While
      msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString()

      End SyncLock
   End If       
 End Sub

.NET Framework
Available since 1.1
Return to top