Control Tree Recursion
page 1 of 1
Published: 10 Mar 2005
Unedited - Community Contributed
"How do I loop through the controls on my web page?" It's not hard to do, but the logic isn't entirely intuitive unless you know a few details.
by Steve Orr
Average Rating: 
Views (Total / Last 10 Days): 13687/ 16

A question I frequently hear from developers is “How do I loop through the controls on my web page?”


There are a variety of reasons a person may want to loop through the controls on a page; perhaps to set a common color or to validate custom business rules.  This kind of thing is not hard to do, but the logic is not entirely intuitive unless you know a few details.


You might already know that every Page has a Controls collection.  From that, you might assume that you can simply loop through this collection to do what you need to all the controls on your page.  You’d be wrong.  A page is a complex tree of controls, and many controls can contain collections of controls themselves, such as a Panel and a Table.  In fact, a Page itself is nothing more than a fancy Control.  (It inherits from, and extends the Control class.)


Since each tree branch can itself have N child branches, the only efficient solution is recursion.  A recursive function is a function that calls itself as many times as necessary to work through any kind of a tree structure.


The following function uses recursion to loop through all the controls on a page and sets the BackColor of all TextBoxes to the specified value. 



private void SetTextBoxBackColor(Control Page, Color clr)


     foreach (Control ctrl in Page.Controls)


          if (ctrl is TextBox)


              ((TextBox)(ctrl)).BackColor = clr;




              if (ctrl.Controls.Count > 0)


                   SetTextBoxBackColor(ctrl, clr);








Private Sub SetTextBoxBackColor(ByVal Page As Control, _

      ByVal clr As Color)


    For Each ctrl As Control In Page.Controls

        If TypeOf ctrl Is TextBox Then

            CType(ctrl, TextBox).BackColor = clr


            If ctrl.Controls.Count > 0 Then

                SetTextBoxBackColor(ctrl, clr)

            End If

        End If


End Sub


The function can be called from pretty much anywhere in your code behind file with a simple line such as this:


SetTextBoxBackColor(Page, Color.Red)


The SetTextBoxBackColor function accepts the Page (or any other container control) as a parameter, along with the color that will be applied to the background of each TextBox within.  The ForEach loop iterates through each control in the collection.  If the Control is a TextBox, it applies the BackColor.  If the control contains other controls, the function calls itself recursively to loop through each of those controls, and so on.


You can take these same principals and modify them in many ways as a potential solution for virtually anything you’d want to do to all your controls or to a certain kind of control.  You now have the secrets you need to tinker with an entire control tree using recursion. 


Steve Orr is a developer and author and can be found at

User Comments

Title: software architect   
Name: Ulas
Date: 2009-12-17 10:41:14 AM
thank u so much
also this code can open a new idea for me example
CRUD manager base
u can dynamicly collect the for each object ID and values
Title: thanks   
Name: tony
Date: 2009-08-12 2:54:51 AM
Good article, it’s help me a lot. thanks
Title: thanks   
Name: tallion
Date: 2008-07-24 10:43:19 AM
just the code i needed
Title: That's part of it   
Name: Rick
Date: 2008-05-29 11:21:04 AM
That's nice, but what if you have nested controls, could you post a recursive example?
Title: It's good   
Name: shruti
Date: 2007-07-14 4:41:51 AM
it's good ,but we can do the same things with the help of theme option.... which is the beeter one?... what is the diffrent between this two?
Title: Good Stuff   
Name: Chris
Date: 2006-07-31 6:12:21 PM
Worked well for me.
Title: Pretty Clear   
Name: [[-SunFlower-]]
Date: 2006-06-26 6:25:26 AM
Its pretty clear and helpful , i adapt this one with text property. Thanks!!
Title: very clear   
Name: Daniel Cramer
Date: 2006-02-03 11:59:54 AM
Thank you for writting a clear and concise article. Much thanks for the clarity..
Title: Control Tree Recursion   
Name: ajitabh
Date: 2005-12-01 8:18:39 AM
well its cool but wat if there are few panels or group boxes are also there to do anything with the text box which is in group box.....or in panel
Title: Good Article   
Name: Anonymous
Date: 2005-11-11 6:23:57 PM
Great job! Very useful!
Title: saved me time   
Name: Ameet
Date: 2005-08-19 2:19:44 AM
Thanks for the tip.
It solved my problem and I didn't have to use any grey matter.
Title: Its Really a helpful Tech Tip   
Name: Ram Kinkar Pandey
Date: 2005-06-01 3:19:49 AM
Its Really a helpful Tech Tip, My need was fulfilled .
Title: Helpful   
Name: Dirk
Date: 2005-05-30 5:08:31 AM
Very helpful article. Thanks!

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-04-14 12:12:49 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search