Unable to FindControl in a Dynamically loaded control



I am trying to access a control (PlaceHolder Control) that is in a
dynamically loaded control. I know it is there as I can see it in my trace.
It may be that it isn't actually loaded yet - but not sure.

In the following page, I am loading a page called "page.aspx" into a control
called pageControl. This page also has an asp:placeHolder control with an

I am trying find this PlaceHolder so I can load another control into it's
controls array. But when I do a FindControl from the Forms Control - it
can't find it. But it is part of Forms Control (I think) as you can see
from the trace.

The message I am getting is: "contentControl is Nothing"

Is this the wrong way to do this?

<%@ Page Language="VB" trace="true" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim thePage as String
Dim pageControl as Control
Dim contentControl as Control
Dim a as htmlControl

thePage = "Page.ascx"
pageControl = LoadControl(thePage)
a = CType(Page.FindControl("addForm"),htmlControl)
contentControl = CType(a.FindControl("Content"),Control)
If ContentControl is Nothing then
trace.warn("contentControl is Nothing")
trace.warn("contentControl is Something")
end if

end Sub

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body id="myBody" leftmargin="0" topmargin="0" marginwidth="0"
marginheight="0" runat="server">
<form id="addForm" runat="server">
<asp:placeHolder ID="thePlaceHolder" runat="server"/>

__PAGE ASP.DisplayCompanyJobs3_aspx
_ctl1 System.Web.UI.LiteralControl
_ctl2 System.Web.UI.LiteralControl
myBody System.Web.UI.HtmlControls.HtmlGenericControl
_ctl3 System.Web.UI.LiteralControl
addForm System.Web.UI.HtmlControls.HtmlForm
_ctl4 System.Web.UI.LiteralControl
thePlaceHolder System.Web.UI.WebControls.PlaceHolder
_ctl0 ASP.ftsPage_ascx
_ctl0:_ctl0 System.Web.UI.ResourceBasedLiteralControl
_ctl0:Navigation System.Web.UI.WebControls.PlaceHolder
_ctl0:_ctl1 System.Web.UI.LiteralControl

_ctl0:Content System.Web.UI.WebControls.PlaceHolder

_ctl0:_ctl2 System.Web.UI.ResourceBasedLiteralControl
_ctl5 System.Web.UI.LiteralControl
UserLoggedOnLabel System.Web.UI.WebControls.Label
_ctl6 System.Web.UI.LiteralControl
UserLoggedOn System.Web.UI.WebControls.Label
_ctl7 System.Web.UI.LiteralControl
_ctl8 System.Web.UI.LiteralControl
_ctl9 System.Web.UI.LiteralControl




I found that I can't use FindControl on an ID of a control of a control.

If I change the FindControl line from:

contentControl = CType(a.FindControl("Content"),Control)


contentControl = CType(a.FindControl("_ctl0:Content"),Control)

it works.

The trace says _ctl0 is "ASP.ftsPage_ascx".

I was confused as to why it wouldn't be something like:

I assume this is because this is a Control in the "thePlaceHolder" control.
Is there a way to give this an ID instead using the _ctl0 ID?



Teemu Keiski


I've explained FindControl in the past.


If you are accessing a control outside of its naming container, you need to
use UniqueId or resolve to the naming container of the control (in this case
the parent of it) and then run FindControl against it. E.g using plain
FindControl looks for controls in the local naming scope. What defines a new
naming scope is if control implement an INamingContainer interface. ascx e.g
a user control is one such.

Controls IDs can be given if you are setting them up e.g on aspx or adding
dynamically in code. However, if they are controls generated by ASP.NET it's
harder to manage. For example autonumbering ctr0...ctrlN is used when
databound controls set up control hierarchies etc etc

Teemu Keiski
AspInsider, ASP.NET MVP


I read your post and it was pretty good.

I looked at the code again and tried this:

contentControl = CType(thePlaceHolder.FindControl("Content"),Control)

I was thinking that now I am now looking at the control "thePlaceHolder" as
the container but that still doesn't work.

My only problem with using ctl0:Content is that if I change the order that
something is loaded, this would change the control to something like

Is there a better way to get the control?



Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question