New Message on dotNET User Group Hyd

Control Tree Recursion - "How do I loop through the controls on my web page?"

Reply
  Reply to Sender   Recommend Message 1 in Discussion
From: Arun Raj.C

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.

<u1:p> </u1:p>

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.)

<u1:p> </u1:p>

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.

<u1:p> </u1:p>

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. 

 

//C#

private void SetTextBoxBackColor(Control Page, Color clr)

{

     foreach (Control ctrl in Page.Controls)

     {

          if (ctrl is TextBox)

          {

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

          }

          else<u1:p></u1:p>

          {

              if (ctrl.Controls.Count > 0)

              {

                   SetTextBoxBackColor(ctrl, clr);

              }

          }

     }

}

 


View other groups in this category.

Click her
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to