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:
Panel
pnlPanel;
// hide all panels
for (i = 0; i < objTabNav.NumTabs; i++)
{
intPostfix = i + 1;
strPageID = "tabPanel" + intPostfix.ToString();
pnlPanel = (Panel)this.FindControl(strPageID);
if (pnlPanel !=
null)
{
pnlPanel.Visible = false;
}
}
My problem is that "pnlPanel" is always null after it looks for and assigns the panel using the FindControl method. The panel ids being passed into the FindControl method are "tabPanel1", "tabPanel2" etc. These panels 100% exist on the page, but the FindControl
method cannot find them. Or there is something wrong with the code above (that worked perfectly in ASP.NET 1.1).
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.
protected void Button1_Click(object sender, EventArgs e)
{
Panel p;
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;
}
}
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
As for the variable ending up null. That's because you can do this
Panel
pnl2 = (Panel)null;
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) -
madaboutnoggins
Member
718 Points
168 Posts
FindControl() problem
Jan 15, 2007 12:31 PM|LINK
Hi There
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:
Panel
pnlPanel;// hide all panels
for (i = 0; i < objTabNav.NumTabs; i++)
{
intPostfix = i + 1;
strPageID = "tabPanel" + intPostfix.ToString();
pnlPanel = (Panel)this.FindControl(strPageID);
if (pnlPanel != null)
{
pnlPanel.Visible = false;
}
}
My problem is that "pnlPanel" is always null after it looks for and assigns the panel using the FindControl method. The panel ids being passed into the FindControl method are "tabPanel1", "tabPanel2" etc. These panels 100% exist on the page, but the FindControl method cannot find them. Or there is something wrong with the code above (that worked perfectly in ASP.NET 1.1).
Anyone have any ideas?
Cheers
Web Monkey
Marian Kostal
Participant
914 Points
191 Posts
Re: FindControl() problem
Jan 15, 2007 01:02 PM|LINK
Enable tracing on your page using
and after you will see tree hierarchy of controls on your page. This can help you to solve your problem.Haissam
All-Star
37421 Points
5632 Posts
Re: FindControl() problem
Jan 15, 2007 01:06 PM|LINK
Dear,
i recommend you to read the below two articles
http://west-wind.com/WebLog/posts/5127.aspx
http://www.odetocode.com/Articles/116.aspx
Happy Coding
MCAD.NET
| Blog |
V-Bot
Member
68 Points
19 Posts
Re: FindControl() problem
Jan 15, 2007 01:06 PM|LINK
I made the following changes to your code and it works fine for me on .NET 1.1:
private void Button1_Click(object sender, System.EventArgs e){
Panel pnlPanel;
// hide all panels for (int i = 0; i < 3; i++){
int intPostfix = i + 1; string strPageID = "Panel" + intPostfix.ToString();pnlPanel = (Panel)
this.FindControl(strPageID); if (pnlPanel != null){
pnlPanel.Visible =
false;}
}
}
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"
Text="Button"></asp:Button>
</form>Panel 1 Panel 2 Panel 3
e_screw
All-Star
19528 Points
3893 Posts
Re: FindControl() problem
Jan 15, 2007 01:49 PM|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):
Thanks
Electronic Screw
Website||Blog||Dub@i.net
Jessica Cao...
All-Star
25329 Points
2567 Posts
Re: FindControl() problem
Jan 16, 2007 02:59 AM|LINK
hi,
this code works fine in asp.net2.0
protected void Button1_Click(object sender, EventArgs e)
{
Panel p;
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;
}
}
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
mbanavige
All-Star
130696 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: FindControl() problem
Jan 16, 2007 03:23 AM|LINK
If there is a MasterPage involved, you may want to read this thread: http://forums.asp.net/thread/1513625.aspx
mokeefe
Star
10850 Points
2098 Posts
Re: FindControl() problem
Jan 16, 2007 03:33 AM|LINK
As for the variable ending up null. That's because you can do this
Panel
pnl2 = (Panel)null;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) -
protected void Page_Load(object sender, EventArgs e)
{
HidePanels(objTabNav);
}
private void HidePanels(Control parent)
{
foreach (Control obj in parent.Controls)
{
if (obj is Panel)
{
Panel pnl = (Panel)obj;
pnl.Visible = false;
}
}
}
Rgds,
Martin.
Martin.
For the benefit of all users please mark any post answers as appropriate.
madaboutnoggins
Member
718 Points
168 Posts
Re: FindControl() problem
Jan 16, 2007 11:56 AM|LINK
Thanks.
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.
Cheers
Web Monkey
vikash2009
Member
2 Points
1 Post
Re: FindControl() problem
Jan 19, 2008 10:35 AM|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");
Response.Write(pnll.ID.ToString());
Please Gimme answer
FindControl() problem