Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 01, 2010 08:32 PM by nkmekal
May 05, 2007 11:12 AM|LINK
I have seen may posts about having problems with the FindControl method. Most seem to come about because the control being searched for is nested within a container other than the webform.
I came across this code (sorry dont remember the web site) that I have posted in response to many of the posts related to this type of problem. I thought it would be easier if I post this code here for others to find.
/// Finds a Control recursively. Note finds the first match that exists
<param name="ContainerCtl">Should be the lowest container in the heirarchy, for eg dont choose Master page if you can pick the specific panel</param>
<param name="IdToFind">ID of the control you are looking for</param>
<returns>the control if found else null</returns>
Control FindControlRecursive(Control Root,
if (Root.ID == Id)
foreach (Control Ctl
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl !=
May 07, 2007 04:37 AM|LINK
thanks for the snippet... would be help ful...
Jun 15, 2007 06:42 PM|LINK
I don't really understand this explanation; it is rather cryptic and I understand C# a little, but I wish it were in VB.
Are you saying that first we need to find the webform, then formview, and then the button in my case? Why do I need todo that, andyway it doesn't work.
I I tried writing this in VB but was stumped as to how you call FindControlRecursive from within itself?
Jun 16, 2007 06:01 AM|LINK
CiscoCylk I have dug up the post that I originally found the code at
http://www.west-wind.com/WebLog/posts/5127.aspx he provides some info on why the problem occurs.
Basically I first hit this problem when I started using some of the new Login control provided with ASP.NET 2.0. It seems like they inlcude an additional container that hides the controls within so that doing a FindControl at the page level doesnt pick them
The code does work for me, on a number of occasions and a number of different applications, so if you would care to post up how you are trying to use the code I could offer an opinion.
Not sure about the 3rd part of your question, as to how I call FindControlRecursive from within itself. Do you mean that you dont understand recursion or is there something else you dont understand
Jun 20, 2007 08:50 AM|LINK
This is true, solution may be your or that:
But recursion is expensive, so better will be:
ctl = ctls.First.Value;
Jun 20, 2007 09:03 AM|LINK
Better version is bellow
private static Control FindControlIterative(Control root, string id)
Control ctl = root;
LinkedList ctls = new LinkedList();
while (ctl != null)
if (ctl.ID == id)
foreach (Control child in ctl.Controls)
if (child.ID == id)
ctl = ctls.First.Value;
Jun 21, 2007 09:23 AM|LINK
Sep 20, 2007 04:57 PM|LINK
Question?? Don't most of the controls you can nest other controls in have a FindControl method?? If you already have the id of the control you want and I assume an idea of the id for the controls container why could you not use the built in methods to search
for this control??
Oct 03, 2007 12:51 PM|LINK
I don't understand how you use the code you posted - where do you type that? Also, can someone translate to VB please?
Oct 05, 2007 10:51 PM|LINK
There's some irony to be noted here. If you read Scottgu's comment at the top, he says that they debated sticking in a recursive FindControl(), but decided against it because of the potential for newbies to start abusing it and thereby negatively affecting
their page's performance. What's ironic is that people went ahead and did what MS didn't want -- wrote their own recursive methods to find controls!
Either way we get the shaft. Use recursion to find a control and you're going to pay for it in terms of performance. Use FindControl("ctl00").FindControl("whatever").FindControl("whatever2") and you won't pay the performance price, but it could come back
to haunt you in the future should you add even one more layer (i.e. - panel, user control, etc.) inbetween.