This won't find nested controls. If these controls are not children of the page, then they won't be found. To resolve this, find the control that is the parent of these panels. For in stance, if the parent is "form1", then this is working code:
protected void Page_Load(object sender, EventArgs e)
{
string texthi = "3";
// FIND THE PARENT
Control form1 = Page.FindControl("form1");
foreach (Panel pnl in form1.Controls.OfType<Panel>())
{
if (pnl.ID.ToUpper() == texthi.ToUpper().Replace(" ", ""))
{
pnl.Visible = true;
}
else
{
pnl.Visible = false;
}
}
}
There are other ways of finding these controls, but this is probably one of the better ones.