« The Region - Microsoft Regional Director's Web Site | Main | Recursive FindControl »

Recursive FindControl(Of T As Control)

I just posted code to allow recursive search for a control anywhere on the page.  Here is the VB (Visual Basic) version:

 

  1 Public Shared Function FindControl(Of T As Control) _
  2     (ByVal startingControl As Control, ByVal id As String) As T
  3     Dim found As T = CType(Nothing, T)
  4     Dim controlCount As Integer = startingControl.Controls.Count
  5     If (controlCount > 0) Then
  6         Dim i As Integer = 0
  7         Do While (i < controlCount)
  8             Dim activeControl As Control = startingControl.Controls.Item(i)
  9             If TypeOf activeControl Is T Then
 10                 found = TryCast(startingControl.Controls.Item(i),T)
 11                 If (String.Compare(id, found.ID, True) = 0) Then
 12                     Return found
 13                 End If
 14                 found = CType(Nothing, T)
 15             Else
 16                 ' Replace "ClassName" with name of this codes class name
 17                 found = ClassName.FindControl(Of T)(activeControl, id)
 18                 If (Not found Is Nothing) Then
 19                     Return found
 20                 End If
 21             End If
 22             i += 1
 23         Loop
 24     End If
 25     Return found
 26 End Function
Posted on Friday, April 13, 2007 at 12:52PM by Registered CommenterJ. Michael Palermo IV in , | CommentsPost a Comment

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.
Editor Permission Required
You must have editing permission for this entry in order to post comments.