Link to home
Start Free TrialLog in
Avatar of eshurak
eshurakFlag for United States of America

asked on

Loop through Panels C#

Using ASP.Net 2.0 and C# I need to loop through all panels on a page. How you tell me how.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please refer this code.... this is for Loop through for Textboxes. try modifting this for Panels.

//C#
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);
              }
          }
     }
}

  'VB.NET
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
        Else
            If ctrl.Controls.Count > 0 Then
                SetTextBoxBackColor(ctrl, clr)
            End If
        End If
    Next
End Sub
Avatar of eshurak

ASKER

Thanks, but how do I get all my panels into the ControlCollection ctls?
You don't have to.  All TOP level controls are in the page's Controls collection, and the recursion makes sure that all the lower level controls are looked at as well.  All the controls are found, but only the controls of type "Panel" cause you to take action.