views:

139

answers:

2

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?

+2  A: 

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);
    }
}
Glennular
A: 

Are the Panels dynamic?

Here is what I tried just now...

  1. Create a master page with only one Place Holder

    <asp:ContentPlaceHolder id="MainContent" runat="server">
    
    
    </asp:ContentPlaceHolder>
    
  2. 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; } }

Rahul Soni