Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Controls.Add() for a Web User Control doesn't instansiate its child controls

Reply
Thread Tools

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

 
 
Ofer Zelig
Guest
Posts: n/a
 
      03-25-2007
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


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      03-25-2007
"Ofer Zelig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.


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

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


 
Reply With Quote
 
 
 
 
Ofer Zelig
Guest
Posts: n/a
 
      03-25-2007
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...


"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Ofer Zelig" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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.

>
> In which method are you adding the control...?
>
> If you're doing it in Page_Load, try doing it in Page_Init instead...
>
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      03-25-2007
"Ofer Zelig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> The host of that user control is a control itself (in this case, a
> "regular", design-mode placed one).


OK.

> 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...


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...


 
Reply With Quote
 
Ofer Zelig
Guest
Posts: n/a
 
      03-25-2007
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.


"Mark Rae" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Ofer Zelig" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > The host of that user control is a control itself (in this case, a
> > "regular", design-mode placed one).

>
> OK.
>
> > 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...

>
> 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...
>
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      03-25-2007
"Ofer Zelig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.


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...


 
Reply With Quote
 
Ofer Zelig
Guest
Posts: n/a
 
      03-26-2007
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!


"Mark Rae" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Ofer Zelig" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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.

>
> 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...
>
>



 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      03-26-2007
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://support.microsoft.com/kb/893667


Sincerely,
Walter Wang ((E-Mail Removed), remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#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://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

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




 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      03-26-2007
"Ofer Zelig" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

>I need some more technical answer, specifically why child controls are not
> available when the parent is created at run-time.


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...


 
Reply With Quote
 
Ofer Zelig
Guest
Posts: n/a
 
      03-26-2007
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...



"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Ofer Zelig" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
> >I need some more technical answer, specifically why child controls are

not
> > available when the parent is created at run-time.

>
> 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...
>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controls.Add() for a Web User Control doesn't instansiate its child controls Ofer Zelig ASP .Net 3 03-28-2007 02:02 AM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
HELP! Child controls of child controls not visible to web app lisa@starways.net ASP .Net Building Controls 0 05-05-2005 09:14 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
can user control change its child control's property using javascript? chaoy@pacbell.net ASP .Net Building Controls 0 08-25-2003 02:31 AM



Advertisments