Unable to FindControl in a Dynamically loaded control

T

tshad

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
"ID=Content".

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"
"http://www.w3.org/TR/html4/loose.dtd">
<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)
thePlaceHolder.Controls.Add(pageControl)
a = CType(Page.FindControl("addForm"),htmlControl)
contentControl = CType(a.FindControl("Content"),Control)
If ContentControl is Nothing then
trace.warn("contentControl is Nothing")
else
trace.warn("contentControl is Something")
end if

end Sub

</script>
<html>
<head>
<title>:</title>
<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"/>
</form>
</body>
</html>
****************************************

__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

Thanks,

Tom
 
T

tshad

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)

to:

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:
thePlaceHolder:Content.

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?

Thanks,

Tom
 
T

Teemu Keiski

Hi,

I've explained FindControl in the past.

http://aspadvice.com/blogs/joteke/archive/2006/08/12/20546.aspx

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
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
 
T

tshad

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
ctl1:Content.

Is there a better way to get the control?

Thanks,

Tom
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top