MBC Computer Solutions Ltd.

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 30 November 2007

Recursively finding controls - where to start?

Posted on 11:15 by Unknown

I love hearing about bugs and problems in components I have authored.  Most people hate hearing about bugs (I assume because they like to think they are perfect), but I like it because it lets me know that people are actually using components I have written, which is always rewarding to know.

I had a report on the CodeProject in response to the article I authored, Persisting the Scroll Position of child DIV's using MS AJAX about a client-side InvalidOperationException during page initialization.   The particular user was embedding my control in a user-control placing two of the controls on the page.  Because my control is an "extender"-type control, one of its properties is the control ID of the other control.  I then use a recursive FindControl method to locate the desired control:

protected virtual Control FindControlRecursive(Control root, string id)
        {
            if(root.ID == id)
            {
                return root;
            }

            foreach(Control c in root.Controls)
            {
                Control t = FindControlRecursive(c, id);
                if(t != null)
                {
                    return t;
                }
            }

            return null;
        }

 

The call to FindControlRecursive looks like:

protected internal Control Control
        {
            get
            {
                if(_control == null)
                {
                    Page page = Page;
                    if(page == null)
                        throw new InvalidOperationException("Page cannot be null");

                    _control = (Control)FindControlRecursive(Page, ControlToPersist);
                    if(_control == null)
                        throw new InvalidOperationException("Could not located the specified control");
                }

                return _control;
            }
        }

Both DIV's in the user-control were named "div1" - can you spot the problem?

_control = (Control)FindControlRecursive(Page, ControlToPersist);

How about now? The problem was I was starting to scan from the Page and all of its children controls - since both DIV's were named "div1", it was always picking up the first control! To resolve this issue I changed Page to Parent:

_control = (Control)FindControlRecursive(Parent, ControlToPersist);

In this case, the Parent is the web-control (or page) hosting the extender control. Lesson Learned: Always start scanning controls from your parent control, not anything higher up in the chain.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • How to change the temperature scale on a Honeywell T6575 Thermostat
    [The complete documentation can be found at http://customer.honeywell.ca/techlit/pdf/95c-00000s/95c-10897.pdf ]   This was bugging me fo...
  • C# – Converting IP’s to Numbers and Numbers to IP’s in 2 lines of code
    I don’t know why everywhere I searched had such complex implementation of this, but converting from a dotted IP to a number (integer) and ba...
  • Why does iTunes setup need to close Outlook?!
    Everytime I update iTunes I remember why I left it so long - the install process is quite annoying! Can someone please explain to me why it...
  • Mac OSX 10.5.2 Freezing Intermittently
    I've been having an issue with my MacBook (you know, that computer I hide under my desk most of the time) where intermittently, the UI w...
  • ScottGu’s Color Scheme for Visual Studio 2010
    ScottGu was nice enough to provide the world with his awesome Visual Studio 2008 color scheme.  I’ve been using this for many years now an...
  • Windows Search 4.0 Released .....and searching finally works!
    I've been dealing with Outlook 2007's search problems since installing it way back then.  Most frequently, I'd search a keyword;...
  • Don’t forget about the defer attribute for non-essential external scripts
    I was recently reviewing a customers eCommerce site and I noticed that the “Please Wait” page that occurs after completing an order but befo...
  • Recursively finding controls - where to start?
    I love hearing about bugs and problems in components I have authored.  Most people hate hearing about bugs (I assume because they like to th...
  • Popup Window Manager
    I was just reading a post by Rick Strahl about managing popup windows in the browser.  I actually authored a mini popup window manager a wh...
  • New Blackberry firmware coming down the pipe?
    My partner alerted me today to the fact that RIM has finally released 4.2 code for the 8700. I complained a while back that the 8800 code ...

Blog Archive

  • ►  2012 (1)
    • ►  February (1)
  • ►  2010 (1)
    • ►  April (1)
  • ►  2009 (7)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  July (1)
    • ►  April (2)
    • ►  February (1)
  • ►  2008 (36)
    • ►  November (3)
    • ►  October (2)
    • ►  September (1)
    • ►  August (1)
    • ►  July (2)
    • ►  June (6)
    • ►  May (4)
    • ►  April (1)
    • ►  March (4)
    • ►  February (7)
    • ►  January (5)
  • ▼  2007 (35)
    • ►  December (1)
    • ▼  November (9)
      • Fixing the window resize event in IE
      • Recursively finding controls - where to start?
      • Don't store request variables in the ViewState!
      • Apple Warranty's aren't so hot
      • MS AJAX Sys.UI.DomEvent documentation missing keyC...
      • Widescreen resolutions & DVI
      • Windows Live Writer is Here!
      • Paul Thurrott's "Inside Windows Server 2008"
      • Mac Trojan In the Wild
    • ►  October (3)
    • ►  September (6)
    • ►  August (7)
    • ►  July (9)
  • ►  2006 (3)
    • ►  May (3)
Powered by Blogger.

About Me

Unknown
View my complete profile