eshurak
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
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
//C#
private void SetTextBoxBackColor(Contro
{
foreach (Control ctrl in Page.Controls)
{
if (ctrl is TextBox)
{
((TextBox)(ctrl)).BackColo
}
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