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
///
</summary>
///
<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>
private static
Control FindControlRecursive(Control Root,
string Id)
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?
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
up.
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
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??
Remeber to mark as Answer if someone's answer helped you.
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.
justSomeone
Participant
1107 Points
245 Posts
Solution to the FindControl problem
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.
/// <summary>
<div class=ForumPostContentText id=ctl00_ctl01_bcr_ctl00___PostRepeater_ctl02_PostViewWrapper> <div class=ForumPostContentText id=ctl00_ctl01_bcr_ctl00___PostRepeater_ctl02_PostViewWrapper>/// Finds a Control recursively. Note finds the first match that exists
/// </summary>
/// <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>
private static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
</div></div>kaushalparik...
All-Star
26568 Points
3688 Posts
MVP
Re: Solution to the FindControl problem
May 07, 2007 04:37 AM|LINK
hi friend...
thanks for the snippet... would be help ful...
[KaushaL] || BloG || Twitter
Don't forget to click "Mark as Answer" on the post that helped you.
CiscoCylk
Member
166 Points
118 Posts
Re: Solution to the FindControl problem
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?
justSomeone
Participant
1107 Points
245 Posts
Re: Solution to the FindControl problem
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 up.
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
stelianx
Member
351 Points
85 Posts
Re: Solution to the FindControl problem
Jun 20, 2007 08:50 AM|LINK
This is true, solution may be your or that:
http://www.codinghorror.com/blog/archives/000307.html
But recursion is expensive, so better will be:
private static Control FindControlIterative(Control root, string id){
Control ctl = root; LinkedList<Control> ctls = new LinkedList<Control>(); while (ctl != null){
if (ctl.ID == id)return ctl;
foreach (Control child in ctl.Controls){
if (child.ID == id) return child;if (child.Controls.Count > 0)ctls.AddLast(child);
}
if (ctls.Count > 0){
ctl = ctls.First.Value;
ctls.Remove(ctl);
}
else ctl = null;}
return null;}
Popa D. Stelian
MCP/MCAD/MCSD
www.simplusoft.net co-founder
ROMANIA
stelianx
Member
351 Points
85 Posts
Re: Solution to the FindControl problem
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) return ctl; foreach (Control child in ctl.Controls) { if (child.ID == id) return child; if (child.HasControls()) ctls.AddLast(child); } ctl = ctls.First.Value; ctls.Remove(ctl); } return null; }Popa D. Stelian
MCP/MCAD/MCSD
www.simplusoft.net co-founder
ROMANIA
stelianx
Member
351 Points
85 Posts
Re: Solution to the FindControl problem
Jun 21, 2007 09:23 AM|LINK
private static Control FindControlIterative(Control root, string id) { Control ctl = root; LinkedList ctls = new LinkedList(); while (ctl != null) { if (ctl.ID == id) return ctl; foreach (Control child in ctl.Controls) { if (child.ID == id) return child; if (child.HasControls()) ctls.AddLast(child); } ctl = ctls.First.Value; ctls.Remove(ctl); } return null; }Popa D. Stelian
MCP/MCAD/MCSD
www.simplusoft.net co-founder
ROMANIA
Rodashar
Member
602 Points
133 Posts
Re: Solution to the FindControl problem
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??
dhassen
Member
439 Points
397 Posts
Re: Solution to the FindControl problem
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?
tysonh28
Member
628 Points
219 Posts
Re: Solution to the FindControl problem
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.