Controls.Add() for a Web User Control doesn't instansiate its child controls

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Mon, 26 Mar 2007 20:55:33


I'll describe the simplest situation of the problem.

I have a simple Web User Control which only contains a: <div id="bla"
runat="server" /> .
I dynamically add it to a page, by performing:

SomeCtl ctl = new SomeCtl();
this.Controls.Add(ctl);

and of course apply a <% @ Register %> tag in order to make SomeCtl known to
the page (is there some more elegant way?).

I override OnPreRender of the control, in order to write some stuff to the
DIV according to some property of the control, but I can't (within the
event code) access the DIV. It is equal to null.
The issue happens of course only when my user control (SomeCtl) is added
dynamically to the page, and not when I place the user control directly
within the page at design time.


Thanks in advance for your help,
Ofer
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Mark Ra » Mon, 26 Mar 2007 21:06:45


In which method are you adding the control...?

If you're doing it in Page_Load, try doing it in Page_Init instead...

 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Mon, 26 Mar 2007 21:13:55

The host of that user control is a control itself (in this case, a
"regular", design-mode placed one).
I can't put it in Page_Init of the host control, because the code must
access properties of the control which are not available in its Page_Load
event yet...
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Mark Ra » Mon, 26 Mar 2007 21:25:55


OK.


In which case, you might find that you need to rethink your logic here a
bit, as I'm pretty sure the problem is that the dynamic creation of the
control is occurring too late in the overall page cycle...

I've seen this many times with dynamically created controls... Create them
in Page_Load, they sometimes work... Create them in Page_Init or
Page_PreInit, they always work...
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Tue, 27 Mar 2007 01:00:57

I can't see a way to change it...
As for the host control (design-time) - as I said, I can't run the
Controls.Add() at its Page_Init event (or any sooner event of course)
because the host control won't be able to access its properties. So it must
run at Page_Load, and in that case - runtime created control's child
controls are always null.






Page_Load
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Mark Ra » Tue, 27 Mar 2007 01:45:12


As I said, I think you'll have to redesign this piece of functionality
because I don't believe what you're trying to do is possible the way you
have architected it...
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Tue, 27 Mar 2007 15:54:26

I need some more technical answer, specifically why child controls are not
available when the parent is created at run-time.
I'll resubmit the question in a new thread, maybe someone can pinpoint the
issue exactly...

Thanks!
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Tue, 27 Mar 2007 15:55:36

I'll describe the simplest situation of the problem.

I have a simple Web User Control which only contains a: <div id="bla"
runat="server" /> .
I dynamically add it to a page, by performing:

SomeCtl ctl = new SomeCtl();
this.Controls.Add(ctl);

and of course apply a <% @ Register %> tag in order to make SomeCtl known to
the page (is there some more elegant way?).

I override OnPreRender of the control, in order to write some stuff to the
DIV according to some property of the control, but I can't (within the
event code) access the DIV. It is equal to null.
The issue happens of course only when my user control (SomeCtl) is added
dynamically to the page, and not when I place the user control directly
within the page at design time.


Thanks in advance for your help,
Ofer
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by wawan » Tue, 27 Mar 2007 15:58:34

Hi Ofer,

I'm not sure if I've fully understood your question, so please feel free to
correct me:

1) You have a web user control which need to load another copy of itself
into its control hierarchy. I think you must have some logic to prevent the
control from recursively loading infinitely, would you please show the
logic here?

2) Normally we use LoadControl to dynamically load a web user control
instead of using "new":

#Overview of user controls vs. custom controls
http://www.yqcomputer.com/


Sincerely,
Walter Wang ( XXXX@XXXXX.COM , remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://www.yqcomputer.com/ #notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://www.yqcomputer.com/
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Mark Ra » Tue, 27 Mar 2007 16:51:35


It's to do with the page creation process...

As I've mentioned (a couple of times now), if dynamic controls are created
in Page_Load they mostly don't work properly because they are created too
late to be wired up correctly etc.

However, if they are created in Page_Init or Page_PreInit, then they pretty
much always work...

I appreciate that you find this annoying because of the way you've designed
things, but there's not much I can do about that...

Posting the same question again won't get you a different answer...
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by wawan » Tue, 27 Mar 2007 17:54:11

Hi Ofer,

It seems you might not have seen my reply in your previous post so I'm
replying here again for your reference:

I'm not sure if I've fully understood your question, so please feel free to
correct me:

1) You have a web user control which need to load another copy of itself
into its control hierarchy. I think you must have some logic to prevent the
control from recursively loading infinitely, would you please show the
logic here?

2) Normally we use LoadControl to dynamically load a web user control
instead of using "new":

#Overview of user controls vs. custom controls
http://www.yqcomputer.com/


Would you please elaborate more on your control so that I can give more
detailed and specific suggestion? Thanks.

Regards,
Walter Wang ( XXXX@XXXXX.COM , remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Wed, 28 Mar 2007 00:08:51

Page_Init or anything similar doens't do the work - I checked.
About redesign, I can't think of a simpler model, so it would be a problem.
All that I have, and need, is a regular user control created at design time,
which in turn creates other user controls at runtime. In that case, as I
said, the runtime-generated control doesn't have children (simple HTML
elements), and it seems to me like a bug in the product itself...







not
pretty
designed
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by wawan » Thu, 29 Mar 2007 11:02:49

Hi Ofer,

As other community members replied, using LoadControl should be the key to
your issue here. Would you please let us know if this suggestion works for
you? Please feel free to let us know if there's anything unclear. Thanks.

Regards,
Walter Wang ( XXXX@XXXXX.COM , remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by Ofer Zeli » Thu, 29 Mar 2007 16:39:38

Thanks Milosz & Walter, for your answer.

LoadControl actually did the job. "new" was the problem.

But - if I omit <% @ Register %> as Milosz suggested, code like SomeCtl ctl
= .... won't compile, because the page doesn't know SomeCtl without this
declaration.
I can write: Control ctl = LoadControl("SomeControl.ascx");
but in that case I won't be able to access ctl's members in the code... so
the only solution as I see would be placing a <% @ Register %> tag, or
declaring the control at the website level at web.config's <controls>
section.

Thanks!




of
you
you

known to
the
 
 
 

Controls.Add() for a Web User Control doesn't instansiate its child controls

Post by wawan » Thu, 29 Mar 2007 18:35:08

Hi Ofer,

Thanks for the update.

Yes LoadControl only returns a Control reference, to use the strong-typed
reference to the actual user control class, you will need to cast it and
you will need to import the namespace of the user control class.

To import the namespace, you will have two approaches: the first one is
doing it like what we do it in C#: put a using in your code. The second one
is yours: <% @ Regiser %> will help you import the namespace.

Actually there's third approach: use full qualified name of the user
control class.

Regards,
Walter Wang ( XXXX@XXXXX.COM , remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.