Page.FindControl for not a full name


Can I find a control using Page.FindControl() if I don't know the full name? I know only part of it, this part is unique, so it won't appear in any other control id.

LVL 21
Who is Participating?
kGeniusConnect With a Mentor Commented:
Haven't tested it, just a thought:

Loop through the controls

Public Function FindControls(ByVal parent As Control) As Control
For each ctrl As Control In parent.Controls
  If ctrl.ID.IndexOf("YOUR PART OF ID") > -1 Then
    return ctrl
    Exit Function
  End If
  If ctrl.HasControls Then
  End If
return Nothing
End Function

maybe still some errors, but it's a first thought
YurichAuthor Commented:
I actually started from this iteration, but apparently in my case it's a bit more complicated as I have not asp-control table on my page, where among other controls, asp and html ones, I have my control. So, it should be more thorough iteration first (checking for not asp controls as they can through exceptions), and second I would use it only if I can't find a way to twick this FindControl function.

can you post your code?
YurichAuthor Commented:
I guess I'll have to iterate after all...

kG, your function is actually not recursive... it will return the first control that has a matching ID and terminate the recursion on that. If I have my control not first, it will not pick it up.

I found a simple way to work out this by using a few if-elses, but still need to do recursion to reset attributes of all controls of this type to default values.

YurichAuthor Commented:
I've done it some time ago already but never had time to get back to the question. Thank you for your input.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.