I want to ask if I can loop in my controls, like textbox, dropdown list or etc, in which they are in a page’s user control. The scenario is that, assume I have a page called ‘Page.aspx’ and I have 3 user controls in that page(uc1, uc2 and uc3) and an asp panel (named PnlTab1) to contain all these controls in each user control.
I am using that code to reach my controls:
UserControl uc1, uc2, uc3;
uc1 = usercontrol1;
uc2 = usercontrol2;
uc3 = usercontrol3;
foreach (Control c in uc1.FindControl("PnlTab1").Controls)
{
if (c is TextBox)
((TextBox)c).Enabled = true;
}
foreach (Control c in uc2.FindControl("PnlTab1").Controls)
{
if (c is TextBox)
((TextBox)c).Enabled = true;
}
foreach (Control c in uc3.FindControl("PnlTab1").Controls)
{
if (c is TextBox)
((TextBox)c).Enabled = true;
}
Now, I do not want to write each time ‘foreach (Control c in uc3.FindControl("PnlTab1").Controls‘ . Can I recursively do that?
Thank you all a lot!
no need for recursion, just another loop: