Recursion in CreateChildControls

I

Iain

In my custom control I have overriden CreateChildControls and the first few
lines look like this.

protected override void CreateChildControls()
{
Debug.WriteLine("CreateChildControls", "FeatureList");
Controls.Clear();
..
}

what I am seeing is that the Controls.Clear line indirectly calls
CreateChildControls (the call seems to be through an internal get on the
Controls collection judging from the stack).

MyUI.DLL!MyUI.FeatureList.CreateChildControls() Line 570 C#
System.Web.dll!System.Web.UI.Control.EnsureChildControls() + 0x87 bytes
System.Web.dll!System.Web.UI.WebControls.CompositeControl.Controls.get() +
0x1c bytes
MyUI.DLL!MyUI.FeatureList.CreateChildControls() Line 571 + 0xb bytes C#

Although the controls end up being right, they are created twice which is
hardly efficient. What do I need to do to stop this happening?

Iain
 
I

Iain

In my custom control I have overriden CreateChildControls and the first few
lines look like this.

protected override void CreateChildControls()
{
Debug.WriteLine("CreateChildControls", "FeatureList");
Controls.Clear();
..
}

what I am seeing is that the Controls.Clear line indirectly calls
CreateChildControls (the call seems to be through an internal get on the
Controls collection judging from the stack).

MyUI.DLL!MyUI.FeatureList.CreateChildControls() Line 570 C#
System.Web.dll!System.Web.UI.Control.EnsureChildControls() + 0x87 bytes
System.Web.dll!System.Web.UI.WebControls.CompositeControl.Controls.get() +
0x1c bytes
MyUI.DLL!MyUI.FeatureList.CreateChildControls() Line 571 + 0xb bytes C#

Although the controls end up being right, they are created twice which is
hardly efficient. What do I need to do to stop this happening?

Iain

The problem here turns out to be that ANY access to the Controls collection
in a CompositeControl causes CreateChildControls to be called.

It seems from the docs that this behaviour is specialised to
CompositeControls. Can anyone offer any insights into this?

Iain
 
I

Iain

The problem here turns out to be that ANY access to the Controls collection
in a CompositeControl causes CreateChildControls to be called.

It seems from the docs that this behaviour is specialised to
CompositeControls. Can anyone offer any insights into this?
So don't inherit from CompositeControl - inherit from WebControl. NOt sure
why I was basing on CompositeControl in the first place - though I think
the templated control samples may have done.


Iain
 
R

Robbe Morris [C# MVP]

Have you run Lutz's Reflector on the .net assembly which
this control is contained in? It would give you the
actual .NET source code so you could see exactly
what it is doing. It "may" not be doing what you
think it is.
 

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