Unable to FindControl in a Dynamically loaded control

Discussion in 'ASP .Net' started by tshad, Jul 31, 2007.

  1. tshad

    tshad Guest

    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, Jul 31, 2007
    #1
    1. Advertising

  2. tshad

    tshad Guest

    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

    "tshad" <> wrote in message
    news:%...
    >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, Jul 31, 2007
    #2
    1. Advertising

  3. tshad

    Teemu Keiski Guest

    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






    "tshad" <> wrote in message
    news:...
    >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
    >
    > "tshad" <> wrote in message
    > news:%...
    >>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
    >>

    >
    >
     
    Teemu Keiski, Jul 31, 2007
    #3
  4. tshad

    tshad Guest

    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


    "Teemu Keiski" <> wrote in message
    news:e1$...
    > 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
    >
    >
    >
    >
    >
    >
    > "tshad" <> wrote in message
    > news:...
    >>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
    >>
    >> "tshad" <> wrote in message
    >> news:%...
    >>>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, Jul 31, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page