Spontaneous Publicity
blogs are the new phone book

How to Traverse the Control Tree in Asp.Net with C# 2.0

September 20, 2006 13:00 by Luke

I couldn't find any easy way to traverse the entire asp .net control tree using the framework classes, so I decided to write my own implementation.  In the end, I wanted to be able to traverse all of the controls and get all which are of a certain type.  First, the method which traverses the tree:

public static void RecurseControls(ControlCollection controls, FindControlDelegate callback) { foreach (Control c in controls) { callback(c); if (c.Controls.Count > 0) { RecurseControls(c.Controls, callback); } } }

This basically recurses through the control tree and calls a given delegate for each control found.  Here is the delegate:

public delegate void FindControlDelegate(Control control);

To top it all off, here is the method I wrote to find all of the controls of a given type on a page:

public static List<T> FindAllControls<T>(Page page) where T : Control { List<T> controls = new List<T>(); RecurseControls(page.Controls, delegate(Control c) { if(c is T) controls.Add(c as T); }); return controls; }

I am usually not one to use cryptic programming techniques such as inline delegates, but in this case it just made sense.  Otherwise, I would have had to somehow share the object containing the list of found controls between two different methods.


Tags:
Categories:
Actions: E-mail | del.icio.us | Permalink | Comments (49) | Comment RSSRSS comment feed

Comments

September 10. 2008 09:31

Dilli

Hey Good work

Dilli

December 10. 2008 10:17

Busby SEO Test

great article, thanks for the nice post!

Busby SEO Test

December 11. 2008 15:09

Belajar SEO

i've been twice reading this article, but still don't get it Frown

Belajar SEO

July 27. 2010 04:40

google sniper warrior

I have bookmarked your site for future visits and hope you make more of these useful posts

google sniper warrior

July 28. 2010 21:05

natural cures for bacterial vaginosis

Magnificent blog post, plenty of valuable information and facts.

natural cures for bacterial vaginosis

August 7. 2010 22:55

meetgirls

I am really not too familiar with this topic but I do like to visit blogs for layout ideas and interesting topics. You really expanded upon a subject that I usually don't care much about and made it very exciting. This is a unique blog that I will take note of. I already bookmarked it for future reference. Cheers

meetgirls

August 8. 2010 00:57

e-cigarette

I prefer to look on the brighter side of things which I'm sure you can appreciate.

e-cigarette

August 10. 2010 16:04

Verona Mangis

Magnificent blog post, a bunch of beneficial details.

Verona Mangis

August 14. 2010 07:36

Claud Capouch

like your site will visit again

Claud Capouch

August 17. 2010 22:52

usb data transfer cable

Only want to say your post is interesting. I must subscribe to your rss feed to keep up to date on forthcoming posts.

usb data transfer cable

August 17. 2010 23:13

dvd to mpeg

Thanks.The post is very informative.I love reading.

dvd to mpeg

August 18. 2010 13:29

iphone accessories

This really needs to be looked at a lot close if we're ever going to find a solution.

iphone accessories

August 21. 2010 06:07

Web Design Company

Awesome! Thank you for sharing.

Web Design Company

August 21. 2010 15:18

Fat Loss for Idiots Review

I think you have spent a lot of time to provide this quality article to us. I don't know how to thank you.

Fat Loss for Idiots Review

August 22. 2010 11:09

Reba Stromski

If you're still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you'll know which is right for you.

Reba Stromski

August 22. 2010 17:28

gfx forums

Great post for us webmasters, thank you for posting it! Cheers!

gfx forums

August 22. 2010 19:10

android devices

Wow thank you for the very informative post, yet again I learned something new about android devices. The whole android scene has really come a long way in the last year. I can't wait till the time when everyone is walking around with an android based phone instead of a darn iphone.

android devices

August 25. 2010 03:58

NFL Football Jerseys

If I speak in the Cheap Authentic NFL Jerseys tongues of men and of angels, but have not love, I am only a resounding gong or a clanging cymbal. If I have the gift of prophecy and can fathom all mysteries and all Official NFL Jerseys knowledge, and if I have a faith that can move mountains, but have not love, I am nothing. If I give all I possess to the poor and surrender my USA Jerseys Shop body to the flames,but have not love, I gain nothing.Love is patient, NFL Jerseys love is kind. It does not envy, it does not boast, it is not proud. It is not rude, it is not self-seeking, it is not easily angered, it NHL Jerseys keeps no record of wrongs. Love does not delight in evil but rejoices with the truth. It always protects, always trusts, always MLB Jerseys hopes, always perseveres.Love never NHL Hockey Jerseys fails. But where there are prophecies, they will cease; where there are tongues, they will be stilled; where there is knowledge, it will pass away. For we know in part and we prophesy in MLB Baseball Jerseys part, but when perfection comes, the imperfect disappea. When I was a child, I talked like a child, I thought like a child, I reasoned like a child. When I became NBA Basketball Jerseys man, I put childish ways behind me. Now we see but a poor reflection as in a mirror; then we shall see face to face. Now I know in part; then I shall know fully, even as I am fully known.And now these three remain: faith, hope and NFL Football Jerseys love. But the greatest of these is love. http://www.jersey-usa.com/sitemap.html LIJ

NFL Football Jerseys

August 27. 2010 11:48

Reduced Glutathione

Extremely inspiring. It's amazing what can be done when we put our minds to it.

Reduced Glutathione

August 27. 2010 11:48

Glutathione

Appreciate what we have, that's what my mother always said to me.

Glutathione

August 27. 2010 11:49

Melatonin

Ha, can't say you're wrong. Just need to get my head around this.

Melatonin

August 27. 2010 11:49

garden-bridge

Ha, actions speak louder than words.

garden-bridge

August 27. 2010 12:26

botkier bags

I have been reading your blog posts during my afternoon break, and I have to admit the entire article has been very informative and very well written. I thought I would let you know that for some reason this blog does not display well in Internet Explorer 8. I wish Microsoft would stop changing stuff. I have a question for you. Would you mind swapping blog roll links? That would be really cool!

botkier bags

August 28. 2010 04:21

Dallas Weelborg

Its really hard to find a good blog, I really enjoy your site, thanks. thank you

Dallas Weelborg

August 28. 2010 08:56

Ashley Linda

why not posting more?

Ashley Linda

August 28. 2010 11:07

Edgar Brzuchalski

do you have an rss feed? I want to add it to my reader but I can't find it...

Edgar Brzuchalski

August 28. 2010 12:33

Rikki Therres

I would love to see some more pictures...

Rikki Therres

August 29. 2010 04:39

seduction affiliate

hey, with so many comments you should add an affiliate program to your blog and make some money...

seduction affiliate

August 29. 2010 23:07

Avery Dhruva

That is such a fantastic resource that you simply are delivering and you give it away for free. I love seeing websites that understand the value of delivering a quality resource for free. It is the old what goes around comes around routine. Did you acquired plenty of links and also I see several trackbacks??

Avery Dhruva

August 30. 2010 15:06

dating affiliates

you may make some money if you add an affiliate program to your blog...

dating affiliates

August 30. 2010 18:35

jokes

nice blog! keep the good work!

jokes

August 31. 2010 02:42

Emily Canson

are you using a custom template?

Emily Canson

August 31. 2010 03:36

ipod accessories

Thanks for the article. I just about passed your blog up in Yahoo but now I'm glad I clicked the link and got to read through it. I'm definitely a lot more informed now. I know quite a few people that will want to check it out. They'll for sure enjoy the heck out of what I just read too. LOL. --Jax

ipod accessories

August 31. 2010 18:22

plymouth business directory

Great post thanks very much for this interesting information.

plymouth business directory

August 31. 2010 20:30

garmin refurbished

I am really not too familiar with this subject but I do like to visit blogs for layout ideas and interesting topics. You actually described a subject that I usually don't care much about and made it very exciting. This is a memorable blog that I will take note of. I already bookmarked it for future reference. Have a great day

garmin refurbished

September 1. 2010 13:56

personal training

don't forgot to get out and exercise...

personal training

September 2. 2010 01:39

ETFs

it will be great if you post more...

ETFs

September 2. 2010 12:55

gift

try to change your template, there are some great ones...

gift

September 2. 2010 16:51

Ashleigh Schreiber

this was a very entertaining read.  i enjoyed it very much!

Ashleigh Schreiber

September 3. 2010 17:41

Glutathione

Extremely inspiring. It's amazing what can be done when we put our minds to it.

Glutathione

September 3. 2010 21:31

mixed wrestling

Thanks for making the sincere effort to divulge this. I feel fairly strong about it and would like to learn more. If it's allright, as you attain more in depth knowledge, would you mind adding more posts similar to this one with more information? It would be extremely helpful and useful for me and my co-workers.

mixed wrestling

September 4. 2010 09:20

ETFs

nice blog, try posting more!

ETFs

September 4. 2010 20:42

chelation

I don't completely agree, but the principle is right, and you're heart is in the right place.

chelation

September 5. 2010 01:09

seduction tips

how old is this post?

seduction tips

September 5. 2010 07:41

B12

Appreciate what we have, that's what my mother always said to me.

B12

September 5. 2010 23:57

ETF newsletter

just bookmarked your blog...

ETF newsletter

September 7. 2010 02:46

iphone radio accessories

Ha, can't say you're wrong. Just need to get my head around this.

iphone radio accessories

September 7. 2010 04:24

Carpet Cleaning Philadelphia

I truly appreciate this blog. Will read on...

Carpet Cleaning Philadelphia

September 8. 2010 08:07

stock newsletter

how old is this post?

stock newsletter

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading