Last post Jan 19, 2008 06:35 AM by vikash2009
Jan 15, 2007 08:31 AM|madaboutnoggins|LINK
I am having problems with the FindControl() method. My aspx page contains 3 panels (but it could be any number).
In my code-behind, I am using a for loop which loops as many times as there are number of panels, checks if the panel exists on the page and set's it's visible property to false. Here is the C# code, I am also using ASP.NET 2.0:
Anyone have any ideas?
Jan 15, 2007 09:02 AM|Marian Kostal|LINK
Enable tracing on your page using
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Trace="true" %>
Jan 15, 2007 09:06 AM|Haissam|LINK
i recommend you to read the below two articles
Jan 15, 2007 09:06 AM|V-Bot|LINK
I made the following changes to your code and it works fine for me on .NET 1.1:
pnlPanel = (Panel)
This is the front end code:
<form id="Form1" method="post" runat="server">
<asp:Panel id="Panel1" style="Z-INDEX: 101; LEFT: 272px; POSITION: absolute; TOP: 96px" runat="server">Panel 1</asp:Panel>
<asp:Panel id="Panel2" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 144px" runat="server">Panel 2</asp:Panel>
<asp:Panel id="Panel3" style="Z-INDEX: 103; LEFT: 288px; POSITION: absolute; TOP: 192px" runat="server">Panel 3</asp:Panel>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 504px; POSITION: absolute; TOP: 96px" runat="server"
</form>Panel 1 Panel 2 Panel 3 </form
Jan 15, 2007 09:49 AM|e_screw|LINK
Seems like you wrote the code in page_load. If you are finding the panels on the page, you need to find them in the
form of the page, as panels are placed inside the form, but not on the page directly.
So its true, you cant find any panel if you use pnlPanel = (Panel) this.FindControl(strPageID); as "this" refers to page, but not the form.
You need to use
pnlPanel = (Panel) this.Form.FindControl(strPageID):
Jan 15, 2007 10:59 PM|Jessica Cao - MSFT|LINK
this code works fine in asp.net2.0
protected void Button1_Click(object sender, EventArgs e)
for (int i = 0; i < 3; i++)
int num = i + 1;
string strID = "Panel" + num.ToString();
p = (Panel)form1.FindControl(strID);
if (p != null)
p.Visible = true;
Jan 15, 2007 11:23 PM|mbanavige|LINK
If there is a MasterPage involved, you may want to read this thread:
Jan 15, 2007 11:33 PM|mokeefe|LINK
As for the variable ending up null. That's because you can do this
Therefore the last cycle is not getting a panel. The reasons for which are not evident by your code sample.
As your panels are direct children of one Control consider removing the ID dependency and just loop through the child controls setting only the Panels like so (likely to be faster than FindControl as it hits each child only once) -
void Page_Load(object sender,
private void HidePanels(Control parent)
foreach (Control obj
if (obj is
Panel pnl = (Panel)obj;
pnl.Visible = false;
Jan 16, 2007 07:56 AM|madaboutnoggins|LINK
Yes, master pages are involved :)
I wasnt using the findcontrol method in the right place, within the master then the form etc. All sorted now.
Jan 19, 2008 06:35 AM|vikash2009|LINK
i have dynamic panel within a panel. i have to select them
but problem in FindControl();
several panel in the main panel. but it found only one panel another panel another panel dont find.
but all are clearly display in page.
Panel chk1 = (Panel)this.FindControl("mainpanel");
Panel pnll = (Panel) chk1.FindControl("pnl1");
Please Gimme answer