FindControl() method and NamingContainer


I created a custom control (ParentCustomControl) which is using a
custom template (implementing ITemplate interface), in the
instantiateIn method of this template I create all the controls I need
(Label, textboxes, button) and I also create another custom control
(ChildCustomControl) which is using a custom template too.

My problem comes when I'm trying to access the child Controls of
ChildCustomControl with the FindControl method. I can access every
others controls with a call like this :

(this.FindControl("MyButton") as ImageButton)

I can also access the ChildCustomControl
(this.FindControl("ChildCustomControl1") as ChildCustomControl)) but
his childs are not accessible... I always get a null reference error,
I think it's could be related to the NamingContainer, because only the
controls created in the the InstantiateIn method of ChildCustomControl
are innaccessible...

Anyone can help ?

Thanks !
Who is Participating?
mirmansoorConnect With a Mentor Commented:
take the child control in an object like this. (i'm not sure abt the syntax)
Dim childCtrl = (this.FindControl("ChildCustomControl1") as ChildCustomControl)) ;
childCtrl.FindControl("childOfChildControlsID") // this might work...
Elvio Lujan.Net Senior DeveloperCommented:
you can public the controls with properties

        Public Property txt1() As TextBox
                Return localTXTBOX
            End Get
            Set(ByVal value As TextBox)
                localTXTBOX = value
            End Set
        End Property
pmartin8Author Commented:
Thank you !

I only had to modify this line
by this one

And it works !!
Thanks again !
anytime... you're most welcome... :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.