Question

This is basically what I'm trying to do.

Lets say my default.aspx page has 6 panels.

<asp:Panel runat="server" ID="1">
<asp:Panel runat="server" ID="2">
<asp:Panel runat="server" ID="3">
<asp:Panel runat="server" ID="4">
<asp:Panel runat="server" ID="5">
<asp:Panel runat="server" ID="6">

Then I have a simple void that accepts string argument of ID.

for example, i pass in ID 3, i want to disable all panels besides panel id ="3"

I tried the following but it's unable to find any Panel controls...

    foreach (Panel pnl in this.Page.Controls.OfType<Panel>())
    {
        if (pnl.ID.ToUpper() == texthi.ToUpper().Replace(" ", ""))
        {
            pnl.Visible = true;
        }
        else
        {
            pnl.Visible = false;
        }

    }
Was it helpful?

Solution

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.

OTHER TIPS

If Panel controls are inside other controls, you won't be able to find it.

If you want to search a control nested inside other control, you need to search recursively.

Here are the help methods -

public static List<T> FindControlsRecursive<T>(Control parent) 
  where T : Control
{
    var foundControls = new List<T>();
    FindControlsRecursive(parent, foundControls);
    return foundControls;
}

public static void FindControlsRecursive<T>(Control parent, List<T> foundControls) 
  where T : Control
{
    foreach (Control control in parent.Controls)
    {
        if (control is T)
            foundControls.Add((T) control);
        else
            FindControlsRecursive(control, foundControls);
    }
}

Usage

<asp:PlaceHolder runat="server" ID="PlaceHolder1">
    <asp:Panel runat="server" ID="Panel1">
        Panel 1
    </asp:Panel>
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="PlaceHolder2">
    <asp:Panel runat="server" ID="Panel2">
        Panel 2
    </asp:Panel>
</asp:PlaceHolder>
<asp:Panel runat="server" ID="Panel3">
    Panel 3
</asp:Panel>

protected void Page_Load(object sender, EventArgs e)
{
    string texthi = "Panel1";

    var panelControls = FindControlsRecursive<Panel>(Page);
    foreach (var panel in panelControls)
    {
        if (panel.ID.ToUpper() == texthi.ToUpper().Replace(" ", ""))
        {
            panel.Visible = true;
        }
        else
        {
            panel.Visible = false;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top