Looping through all control in one page

This code will help you to loop through all control and child control in one page. To call the Following function you can use something like SetTextBoxBackColor(this.Page,Color.Yellow) in your code behind.

 private void SetTextBoxBackColor(Control Page, Color clr)
    {
        foreach (Control ctrl in Page.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)(ctrl)).BackColor = clr;
            }
            else
            {
                if (ctrl.Controls.Count > 0)
                {
                    SetTextBoxBackColor(ctrl, clr);
                }
            }
        }
    }

Reference:

http://steveorr.net/faq/controltreerecursion.aspx

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.