Answered by:
FindControl not working in content page

Question
-
User1900948409 posted
i have the following:
<asp:Content ContentPlaceHolderID="plcMainContent" ID="cntMain" runat="server">
...
<asp:Panel id="pnlThumb1" runat="server" Visible="true"/>
...
</asp:Content>
if i call this:
this.pnlThumb1.Visible = false;
no problems.
but if i call this:
this.FindControl("pnlThumb1").Visible = false;
i get a NullReferenceException telling me it can't find it.
i thought maybe it was because it's in a content place holder (FindControl only works on the top level control), but there's not way to reference the "cntMain." so i can't do cntMain.FindControl.
any ideas?Monday, June 19, 2006 7:00 PM
Answers
-
User1900948409 posted
ok, i figured this out. it's really a cr**py implementation in my opion by the ASP.NET team, but this is what you have to do.
1) in your content page, add the following:
<%@ MasterType TypeName="MasterDefault" %>
where "MasterDefault" is the class name of your master file.
you don't actually have to do this, but if you don't, when you reference this.Master on the page you're working on, it'll actually give you the MasterPage class, rather than your own masterpage type. ok, weird. by adding this you save yourself a few lines of code in the page.
2) get a reference to your content place holder:
ContentPlaceHolder mainContent = (ContentPlaceHolder)this.Master.FindControl("plcMainContent");
NOTE: when you do find control, you need to put in the ContentPlaceHolderID NOT the ID of the ContentPlace holder. backwards enough for you? as an example, this is my <asp:Content> tag:
<asp:Content ContentPlaceHolderID="plcMainContent" ID="cntMain" runat="server">
3) once you have the refernce to that content holder, run FindControl as you normally would:
mainContent.FindControl("pnlThumb1").Visible = false;
so in conclusion, you:
add:
<%@ MasterType TypeName="MasterDefault" %>
to your .aspx content page
and do this in your code:ContentPlaceHolder mainContent = (ContentPlaceHolder)this.Master.FindControl("plcMainContent");
mainContent.FindControl("pnlThumb1").Visible = false;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 19, 2006 8:31 PM
All replies
-
User1900948409 posted
ok, i figured this out. it's really a cr**py implementation in my opion by the ASP.NET team, but this is what you have to do.
1) in your content page, add the following:
<%@ MasterType TypeName="MasterDefault" %>
where "MasterDefault" is the class name of your master file.
you don't actually have to do this, but if you don't, when you reference this.Master on the page you're working on, it'll actually give you the MasterPage class, rather than your own masterpage type. ok, weird. by adding this you save yourself a few lines of code in the page.
2) get a reference to your content place holder:
ContentPlaceHolder mainContent = (ContentPlaceHolder)this.Master.FindControl("plcMainContent");
NOTE: when you do find control, you need to put in the ContentPlaceHolderID NOT the ID of the ContentPlace holder. backwards enough for you? as an example, this is my <asp:Content> tag:
<asp:Content ContentPlaceHolderID="plcMainContent" ID="cntMain" runat="server">
3) once you have the refernce to that content holder, run FindControl as you normally would:
mainContent.FindControl("pnlThumb1").Visible = false;
so in conclusion, you:
add:
<%@ MasterType TypeName="MasterDefault" %>
to your .aspx content page
and do this in your code:ContentPlaceHolder mainContent = (ContentPlaceHolder)this.Master.FindControl("plcMainContent");
mainContent.FindControl("pnlThumb1").Visible = false;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 19, 2006 8:31 PM -
User686179636 posted
Thanks for sharing !!!
Tuesday, June 19, 2007 5:37 PM -
User-1107059751 posted
This was very helpful review. Thanks for sharing it with us.
Nirmala
Tuesday, July 17, 2007 8:44 AM -
User43235592 posted
Thanks..dear it poped me out from a stuck.
its really helpful
Balpreet Patil (balpreet.patil@gmail.com)
Friday, September 14, 2007 6:42 AM -
User2013597856 posted
Good reply, it's probably a good idea to note that this is due to the late binding approach that you are using; however, you could also early bind this object/control as well:
pnlThumb1.Visible = false;
k
Thursday, October 4, 2007 12:10 PM -
User-919465860 posted
Thanks a lot dear friend.
Friday, December 28, 2007 9:36 AM -
User315369078 posted
Thank you!
Wednesday, June 25, 2008 3:19 PM -
User-212733016 posted
You could create a more elegant solution by using a bit of reflection combined with extension classes:
public static class PageExtensions { public static T GetControl<T>(this Page page, string name) where T : Control { FieldInfo field = page.GetType().GetField(name, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); if (field != null) return (T)field.GetValue(page); return null; } }
The call would then be
this.GetControl<Panel>("pnlThumb1").Visible = false;
Please find the complete sample with description on my blog
http://www.iucon.biz/blog/index.php/2008/10/10/solution-for-findcontrol-with-masterpages/
Friday, October 10, 2008 9:47 AM -
User-1527108608 posted
thanks so much
Thursday, July 30, 2009 2:23 PM -
User1014984822 posted
Thank you so much for this answer. It got my FindControl working !!!
Friday, March 12, 2010 2:32 PM -
User-2118676656 posted
Another very succint, simple solution may be foud here:
Thursday, July 1, 2010 9:37 AM -
User-532303751 posted
Wow. Thanks so much.
I'm dynamically creating controls in the Page Init event of a content page and was very surprised to find that I could not reference the controls I created. This will save me a LOT of time.
Monday, June 6, 2011 4:30 PM