Logo




Subscribe:
RSS 2.0 | Atom 1.0
Categories:

Sign In


[Giagnocavo]Michael::Write()

 Tuesday, August 02, 2005
Best way to traverse all controls on an ASP.NET page?

I was working on an application today, and I needed to add some data to every HyperLink on the ASP.NET page (for a custom authorization string). I thought it might be a common thing: needing to go through all the controls on a page, but apparently not. I didn't find any framework functionality, and the only code samples (just to see if I have the “best” way of doing things) led to some not-so-nice code (arraylists and recursion!). So, here's the best I've come up with (criticism, please):

        Stack<Control> remainingControls = new Stack<Control>();
        remainingControls.Push(this);
        do
        {
            Control currentControl = remainingControls.Pop();
            foreach (Control item in currentControl.Controls)
            {
                if (item is HyperLink)
                {
                    HyperLink hl = (HyperLink)item;
                    hl.NavigateUrl = AddAuthToUrl(hl.NavigateUrl);
                }
                else if (item.Controls.Count > 0)
                {
                    remainingControls.Push(item);
                }
            }
        } while (remainingControls.Count != 0);

Code
Tuesday, August 02, 2005 9:52:15 PM UTC  #    Comments [3]  |  Trackback Tracked by:
"http://nt0poev.biz/solaris.html" (http://nt0poev.biz/solaris.html) [Pingback]


Wednesday, August 03, 2005 1:19:00 AM UTC
Hmm, I sorta picked up on not using recursion unless I really have to. Not that the performance is going to be critical or anything... I guess it's more of a personal preference?
Wednesday, August 03, 2005 6:15:18 AM UTC
If you want to traverse a tree - which is what the control tree is... - you should use recursion - that's essenially what it was made for. Directly or indirectly, it's how ASP.NET gets through all your controls as well.

There's two bigger problems I see. One, when you recurse anything you've got to recurse EVERYTHING. Whether you're using recursing or faking it with a stack, you have to look at every single control on the page.

More importantly, though, you are tweaking controls on your page with code which is no telling where. If I had to go in after you and find this code, I would have no idea where to look.

The way I did this (for a premium content model) was to create a new hyperlink control that derives from HyperLink. In the PreRender event, I simply see if the user has access to the content, in which case the hyperlink stays the same, or if he doesn't, I change the link to the "buy" page.

This is a much more modular approach. The code which modifies your values are contained within the control with that value. Plus, there's no need to re-traverse code that ASP.NET is already recursing for you. ASP.NET recurses your control tree 5 or 6 times as it is... one for each of the events, Init, Load, PreRender, Render, etc...

-Scott
Wednesday, August 03, 2005 11:15:36 AM UTC
Deriving a control -- yea, that would definately give much closer control over how things would get handled (and save some rendering time too!). If my pages were any more complex, I'd definately steal your idea :).
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview