Reputation: 15754
I'm trying to hide all panels on a page, when a button click occurs.
This is on a web content form, within a master page.
The contentplageholder is named: MainContent
So I have:
foreach (Control c in Page.Form.FindControl("MainContent").Controls) {
if (c is Panel) {
c.Visible = false;
}
}
This never find any panels. The panels are within an Update Panel, and I tried
foreach(Control c in updatePanel.Controls) { }
and this didn't work either. I also tried :
foreach(Control c in Page.Controls) { }
and that didn't work either.
Any idea what I'm missing here?
Upvotes: 1
Views: 1606
Reputation: 18215
you have to recursively traverse the control tree
HidePanels(Page.Form.FindControl("MainContent"))
void HidePanels(Control parentControl){
foreach (Control c in parentControl.Controls) {
if (c is Panel)
c.Visible = false;
if (c.Controls.Count > 0)
HidePanels(c);
}
}
Upvotes: 2
Reputation: 4968
Are the Panels dynamic?
Here is what I tried just now...
Create a master page with only one Place Holder
<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>
In default.aspx, added two Panels and Button, and your first code snipped worked just fine...
foreach (Control c in Page.Form.FindControl("MainContent").Controls) { if (c is Panel) { c.Visible = false; } }
Upvotes: 0