Page.FindControl for not a full name

Posted on 2006-06-20
Last Modified: 2012-06-22

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.

Question by:Yurich
  • 3
  • 2

Accepted Solution

kGenius earned 500 total points
ID: 16941166
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
LVL 21

Author Comment

ID: 16941217
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.


Expert Comment

ID: 16942166
can you post your code?
LVL 21

Author Comment

ID: 16958869
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.

LVL 21

Author Comment

ID: 17034169
I've done it some time ago already but never had time to get back to the question. Thank you for your input.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Json and mvc 2 43
Allow space in this pattern 2 46
Resolve Dependency Issues 4 41
Host pages 5 25
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now