FindControl() method and NamingContainer

D

Dan

Hi,

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 !
 
G

Guest

Dan,

Due to performance reasons FindControl method does not perform recursive
search, which means if you want to find a control within instantiated
template, you have to call FindControl of the container. In your case you
first obtain Child

ChildCustomControl ctrl = this.FindControl("ChildCustomControl1") as
ChildCustomControl;
TextBox txt = ctrl.FindControl("whatever") as TextBox;

Also, make sure all the controls are created and template instantiated in
CreateChildControls(), and call EnsureChildControls always before refreing
control contained in the template.

Hope this helps
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top