Find a control in a hierarchy of controls

20 Nov

You a control ID and you need to find it within a hierarchy of controls. you can use this function:


Private Function FindControlRecursive(ByRef callingControl As System.Web.UI.Control, ByVal controlId As String) As System.Web.UI.Control
    Dim ctrl As Control = Nothing
        If callingControl Is Nothing Then Return Nothing

        ctrl = callingControl.FindControl(controlId)

        If ctrl Is Nothing Then
            For Each child As Control In callingControl.Controls
                ctrl = FindControlRecursive(child, controlId)
                If ctrl IsNot Nothing Then Exit For
        End If

    Catch ex As Exception

    End Try
    Return ctrl

End Function

Leave a comment

Posted by on November 20, 2009 in Programming


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: