My Cascading Nested FindControl Function

5. June 2009

Read this article in your language IT | EN | DE | ES

There are many examples out there of cascading methods to find a control nested down in another control which is in some other control.  So here’s my version.  Maybe there’s better ways, but this one works for me.  Returns Nothing if it’s not found. So capture for that.

''' <summary>
''' Does a FindControl on the given Control and if not found,
''' cascades down into the controls of the control until
''' found or no more controls and returns nothing
''' </summary>
''' <param name="StartingControl">Control to start with</param>
''' <param name="FindThisID">ID of control you are looking for</param>
''' <returns></returns>
''' <remarks></remarks>
Private Function FindControlCascading(ByVal StartingControl As Control, _
ByVal FindThisID As String) As Control
Try
If StartingControl.FindControl(FindThisID) Is Nothing Then
If StartingControl.Controls.Count > 0 Then
For Each objControl In StartingControl.Controls
FindControlCascading(objControl, FindThisID)
Next
End If
Else
Return StartingControl.FindControl(FindThisID)
End If
Return Nothing
Catch ex As Exception
Throw ex
End Try
End Function

Got a better way? I’d love to hear it.

Code Snippets, Programming, VB.NET ,

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading