About the author

Miron Abramson
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments



The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2018

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Collect all controls from specific type

I got a simple "mission" to make a method that collect all the TextBox controls from any given control on the page (or from the Page itself).  As I like to do things in the most 'Generic' way, I finished with a simple method that collect all contols from any type you want.

This is the method that run recursive and collect all the controls from type T: 

        /// <summary>
        /// Loot recursive and collect the controls
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="contol"></param>
        /// <param name="list"></param>
        private static void LoopControls<T>(Control parentContol, List<T> list) where T : Control
            foreach (Control ctrl in parentContol.Controls)
                if (ctrl is T)
                    list.Add(ctrl as T);
                    if (ctrl.Controls.Count > 0)
                        LoopControls(ctrl, list);

To start the 'collecting' process:

        /// <summary>
        /// Collect all the controls from type 'T'
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="contol"></param>
        /// <returns></returns>
        public static List<T> CollectControls<T>(Control parent) where T : Control
            List<T> list = new List<T>();
            LoopControls(parent, list);
            return list;

Drop this method in your 'Utilities' class, and its ready to go.  Now, To collect  all the TextBox in the page, just us it like this:

System.Collections.Generic.List<TextBox> l = Utils.CollectControls<TextBox>(Page);

and to collect all the Labels, just change the  '<TextBox>' to '<Label>'.

Using 'Generic' is very goot thing, and a lot of times, if you thing on the feutre and not focus only on the current mission when you write any method, you will end up using the Generic namespace.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | C# | Server side
Posted by Miron on Wednesday, February 06, 2008 8:50 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Related posts



Wednesday, February 06, 2008 9:11 PM


Trackback from DotNetKicks.com

Collect all controls from specific type using generics

Daniel ca

Sunday, March 09, 2008 11:45 PM


This looks like a nice substitute for C# 3.0's .OfType<T> extension method.

Miron il

Sunday, March 23, 2008 10:19 AM


Hi Daniel,
Not exactly. My method will find ALL controls from a given type in the parent given control, while the extension .OfType<T> will find only the controls in the 'first level'. I mean, it will not find controls that are inside other controls.