Page view counter

Solution to the FindControl problem

Rate It (5)

Last post 02-23-2009 12:46 AM by david wendelken. 31 replies.

Sort Posts:

  • Solution to the FindControl problem

    05-05-2007, 7:12 AM
    • Loading...
    • justSomeone
    • Joined on 02-10-2007, 11:46 AM
    • justSomewhere
    • Posts 245
    • Points 1,107

    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>

    /// 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;

    }

  • Re: Solution to the FindControl problem

    05-05-2007, 8:47 AM
    • Loading...
    • kimoy
    • Joined on 02-13-2007, 10:24 AM
    • Posts 154
    • Points 684
    funny, i just posted the same code minutes ago to somebody in a different thread.
  • Re: Solution to the FindControl problem

    05-07-2007, 12:37 AM

    hi friend...

    thanks for the snippet... would be help ful... 

    Thanx,
    [KaushaL] || BloG || Microsoft MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: Solution to the FindControl problem

    06-15-2007, 2:42 PM
    • Loading...
    • CiscoCylk
    • Joined on 04-20-2007, 3:41 PM
    • Posts 109
    • Points 140

    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?

     

  • Re: Solution to the FindControl problem

    06-16-2007, 2:01 AM
    • Loading...
    • justSomeone
    • Joined on 02-10-2007, 11:46 AM
    • justSomewhere
    • Posts 245
    • Points 1,107

    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

  • Re: Solution to the FindControl problem

    06-20-2007, 4:50 AM
    • Loading...
    • stelianx
    • Joined on 11-07-2006, 12:57 AM
    • Bucharest or Paris
    • Posts 85
    • Points 351

    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;

    }

    Yours,
    Popa D. Stelian
    MCP/MCAD/MCSD
    www.simplusoft.net co-founder
    ROMANIA
  • Re: Solution to the FindControl problem

    06-20-2007, 5:03 AM
    • Loading...
    • stelianx
    • Joined on 11-07-2006, 12:57 AM
    • Bucharest or Paris
    • Posts 85
    • Points 351

    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;
            }
     
    Yours,
    Popa D. Stelian
    MCP/MCAD/MCSD
    www.simplusoft.net co-founder
    ROMANIA
  • Re: Solution to the FindControl problem

    06-21-2007, 5:23 AM
    • Loading...
    • stelianx
    • Joined on 11-07-2006, 12:57 AM
    • Bucharest or Paris
    • Posts 85
    • Points 351

     

    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;
            }
     
    Yours,
    Popa D. Stelian
    MCP/MCAD/MCSD
    www.simplusoft.net co-founder
    ROMANIA
  • Re: Solution to the FindControl problem

    09-20-2007, 12:57 PM
    • Loading...
    • Rodashar
    • Joined on 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129
    • Points 574

    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.
  • Re: Solution to the FindControl problem

    10-03-2007, 8:51 AM
    • Loading...
    • dhassen
    • Joined on 07-09-2007, 7:38 AM
    • Posts 396
    • Points 439

     I don't understand how you use the code you posted - where do you type that? Also, can someone translate to VB please?

  • Re: Solution to the FindControl problem

    10-05-2007, 6:51 PM
    • Loading...
    • tysonh28
    • Joined on 10-17-2006, 9:23 PM
    • Home of the GOOD potatoes
    • Posts 210
    • Points 599

    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.

    This is the part where I politely request you to mark my response as the Answer, under the pretense that doing so will greatly help the community, blah, blah...
    When in actuality I'm singlely intent upon the wopping 10 points that I'll receive!
    ... Oops, was that my outloud voice? ;)
  • Re: Solution to the FindControl problem

    10-16-2007, 8:21 AM
    • Loading...
    • bassplayer69
    • Joined on 10-05-2007, 12:26 PM
    • Posts 14
    • Points 6

    dhassen:

     I don't understand how you use the code you posted - where do you type that? Also, can someone translate to VB please?

     

     

    Here You go:

     

    	Private Shared Function FindControlIterative(ByVal root As Control, _
    		ByVal id As String) As Control
    
    		Dim ctl As Control = root
    		Dim ctls As LinkedList(Of Control) = New LinkedList(Of Control)
    
    		Do While (ctl IsNot Nothing)
    			If ctl.ID = id Then
    				Return ctl
    			End If
    			For Each child As Control In ctl.Controls
    				If child.ID = id Then
    					Return child
    				End If
    				If child.HasControls() Then
    					ctls.AddLast(child)
    				End If
    			Next
    			ctl = ctls.First.Value
    			ctls.Remove(ctl)
    		Loop
    
    		Return Nothing
    
    	End Function
    

      Make sure you add:

     

    Imports System.Collections.Generic at the top of your code.

     

     

  • Re: Solution to the FindControl problem

    11-10-2007, 10:02 AM
    • Loading...
    • chalamarc
    • Joined on 11-10-2007, 2:43 PM
    • Posts 24
    • Points 88

    i think using of findcontrol() on your formview or anyother control name, it is the best way as you neednot to actually go down on your performance and also you can get rid of all the code that has been shown on the top and one which make full use of the api's and its functions which microsoft provides.

    C.C.Reddy
  • Re: Solution to the FindControl problem

    11-10-2007, 11:28 AM
    • Loading...
    • david wendelken
    • Joined on 07-27-2005, 7:47 PM
    • Addis Abeba, Ethiopia
    • Posts 1,924
    • Points 11,935
    • Moderator

    chalamarc:

    i think using of findcontrol() on your formview or anyother control name, it is the best way as you neednot to actually go down on your performance and also you can get rid of all the code that has been shown on the top and one which make full use of the api's and its functions which microsoft provides.

     

    If that were a valid argument, we would still be coding in COBOL and FORTRAN, or maybe 1s and 0s. :) 

    Computer Science is the science of giving away something you want to get something else you want more.

    Which route to use is simply a matter of priorities.  The recursive (or iterative) versions of FindControl provide the following benefits and drawbacks:

    -  Runtime Performance
    + Codetime Performance (it's faster and more convenient for the programmer!)
    + Robustness when the number of levels in the UI Layer changes
    + Easier code to read.

    Hard-wiring the reference by nesting chaining FindControl methods offers the following benefits and drawbacks:

    + Runtime Performance
    -  Codetime Performance
    -  Robustness
    -  Harder to read.

    Pick the method that best meets your needs.

     

    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: Solution to the FindControl problem

    11-14-2007, 1:50 PM
    • Loading...
    • ks2007
    • Joined on 09-24-2007, 9:14 PM
    • Posts 280
    • Points 455

    I am new to ASP.net and I would like to know how you would actually use the above method. Could you please give an example?

    Thanks.

    If this answered your question, be sure to mark it as the answer.
Page 1 of 3 (32 items) 1 2 3 Next >