Jack Marchetti
Jack Marchetti

Reputation: 15754

Hiding all panels on a web content form within a master page

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

Answers (2)

Glennular
Glennular

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

Rahul Soni
Rahul Soni

Reputation: 4968

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; } }

Upvotes: 0

Related Questions