Groups | Blog | Home
all groups > asp.net building controls > november 2003 >

asp.net building controls : CreateChildControls not being called


Colin Young
11/24/2003 1:59:26 PM
Has anybody ever seen a situation in a custom control where
CreateChildControls is not being called? I've set a break point in the
method and it is never hit (in addition, none of the controls that are
supposed to be created are not being created so I'm reasonably certain it
isn't just a problem with the debugger not showing the correct code or
anything like that).

Thanks

Colin

Jimmy [Used-Disks]
11/24/2003 2:13:41 PM
[quoted text, click to view]

Are you calling EnsureChildControls() in the right places?

[quoted text, click to view]

--
-Jimmy

Colin Young
11/25/2003 9:59:34 AM
As far as I can tell. The interesting thing is that the control is basically
identical to another control that doesn't call EnsureChildControls()
anywhere, yet it works. My understanding is that I only need to call it if I
am trying to manipulate the properties of any of the controls that make up
my control, and since I am not directly manipulating the properties, I
shouldn't need to call it myself (i.e. the framework will take care of it
when necessary).

I've solved the problem by calling EnsureChildControls() in the render
method.

Colin

[quoted text, click to view]

Jimmy [Used-Disks]
11/25/2003 10:46:17 AM
[quoted text, click to view]

Well, the difference is in there somewhere.

[quoted text, click to view]

I don't beleive so. I could be wrong though.

[quoted text, click to view]

I suggest moving it to the Controls property:

public override Controls{
get{
EnsureChildControls();
return base.Controls;
}
}

[quoted text, click to view]

--
-Jimmy

Colin Young
11/25/2003 1:36:55 PM
It was already in the controls property. That's why I find it so odd that it
wasn't working. I've written at least 10 or 15 custom controls and I've
never had this problem before. Render is the only method that appears to be
getting called reliably, although now that I think about it, it may have
something to do with how I am creating the control and adding it to the
page. I'm using some old code I wrote when I was first learning the
framework (in fact before I knew what a custom control was or how to write
one), so that may be the problem.

Thanks.

Colin

[quoted text, click to view]

John Saunders
11/25/2003 3:04:36 PM
Maybe you answered this already, but, in what event did you load the
control?

Also, EnsureChildControls is supposedly called before the PreRender event
fires.

--
John Saunders
John.Saunders at SurfControl.com


[quoted text, click to view]

Colin Young
11/25/2003 4:52:34 PM
Well, see, that was the problem. As I said, I was using some page templating
code I wrote way back when I was just learning .Net, and prior to learning
custom controls properly. It turns out that in my template code I only made
use of "special" custom controls because I was only calling the Render
method, rather than adding the custom control to the control tree for the
page.

It just tells me it's time to revisit my header control and fix it.

Colin

[quoted text, click to view]

John Saunders
11/25/2003 5:24:44 PM
[quoted text, click to view]

Colin, that sounds familiar!

FYI, in case you didn't know, the next version of ASP.NET (in the "Whidbey"
release) will include a built-in page templating technology (master pages).
I've played with it a bit, and it looks pretty cool.

In particular, if one currently has "content" which is stored in user
controls, it should be easy to adapt this content for use with master pages.
This made me feel _much_ better, since my page templating code uses user
controls both for the content and for the "master".
--
John Saunders
John.Saunders at SurfControl.com

Colin Young
11/26/2003 10:09:24 AM
I've read up on the master pages in Whidbey, but unfortunately my projects
can't wait until it gets released :) The good news is the master pages
implementation looks very similar to my own solution so hopefully I won't
need to change too much code.

Colin

[quoted text, click to view]

AddThis Social Bookmark Button