Recursive FindContol (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
 Thanks to Steve Smith for the inspiring code sample.

4 Comments

  • But Mike,

    Why would you do that with generics in 26 lines of code when you can do it without in 12? I would think that Generics would make this easier, not harder...


    #Region " FindControlRecursive "

    '''
    ''' Finds a Control recursively. Note finds the first match and exists
    '''
    '''

    '''

    '''
    Public Shared Function FindControlRecursive(ByVal Root As Control, ByVal Id As String) As Control
    If Root.ID = Id Then
    Return Root
    End If
    For Each Ctl As Control In Root.Controls
    Dim FoundCtl As Control = FindControlRecursive(Ctl, Id)
    If Not (FoundCtl Is Nothing) Then
    Return FoundCtl
    End If
    Next
    Return Nothing
    End Function

    #End Region

  • This is also a nice alternative

  • Robert was sort of right, we don't need generics since we are dealing with base controls. However, there were 2 points missed... I found the C# version first and didn't even know about the VB version before I had converted it.

    The C# version also has an Overload for passing only the ID.

    Secondly, if this is put in a Page Class you need to Shadow the Page's FindControl Method so this one takes over.

    So... the corrected way to do all this in 10 Lines of code is:

    Public Shadows Function FindControl(ByVal id As String) As Control
    Return FindControl(Page, id)
    End Function
    Public Shared Shadows Function FindControl(ByVal startingControl As Control, ByVal id As String) As Control
    For Each ctl As Control In startingControl.Controls
    If String.Compare(id, ctl.ID, True) = 0 Then Return ctl
    Dim found = FindControl(ctl, id)
    If found IsNot Nothing Then Return found
    Next
    Return Nothing
    End Function

    :-)

    This works great on Master Pages which are always troublesome with Finding Controls unless you remember you are in a MP.

  • Thanks! This saves me a bunch of trouble, I appreciate the work on this. Great Blog!

Comments have been disabled for this content.