Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

Unable to FindControl in a Dynamically loaded control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 31 Jul 2007 00:22 GMT
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
tshad - 31 Jul 2007 00:42 GMT
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

>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
[quoted text clipped - 79 lines]
>
> Tom
Teemu Keiski - 31 Jul 2007 17:11 GMT
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

Signature

Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

>I found that I can't use FindControl on an ID of a control of a control.
>
[quoted text clipped - 103 lines]
>>
>> Tom
tshad - 31 Jul 2007 17:52 GMT
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

> Hi,
>
[quoted text clipped - 123 lines]
>>>
>>> Tom

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.