I need to find a control in one page but I don't know the complete ID. I only know a part of the ID.
What I wan't to do is something like this:
control = Page.FindControl(part1 & part2)
Where part1 is the known part of the ID and part2 is the unknown part.
For what it's worth, you could use this extension method which searches all child controls:
Module ControlExtensions
<Runtime.CompilerServices.Extension()>
Public Function FindControlPart(root As Control, IdStart As String) As Control
Dim controls As New Stack(Of Control)(root.Controls.Cast(Of Control)())
While controls.Count > 0
Dim currentControl As Control = controls.Pop()
If currentControl.ID.StartsWith(IdStart, StringComparison.InvariantCultureIgnoreCase) Then
Return currentControl
End If
For Each child As Control In currentControl.Controls
controls.Push(child)
Next
End While
Return Nothing
End Function
End Module
Usage:
Dim control As Control = Page.FindControlPart(part1)
It returns the first control which start with a given ID-Part. So it's possible that you get the wrong. It's less error-prone if you use the correct NamingContainer
instead of the Page
as root.