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


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

Regards,
Walter Wang ((E-Mail Removed), 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.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      03-28-2007
Good morning,

Web user controls have to be instantiated by calling LoadControl() method of
the page. Replace
SomeCtl ctl = new SomeCtl();
this.Controls.Add(ctl);

with:

// make sure path is correct
SomeCtl ctl = (SomeCtl) LoadControl("SomeControl.ascx");
this.Controls.Add(ctl);

Then, calling FindControl() will return valid HtmlGenericControl instance
representing your div. In addition, if you load controls programatically, you
don't need to use @Register directive on the page. It's only necessary if you
use put web user control in aspx code.

hope this helps

--
Milosz


"Ofer Zelig" wrote:

> 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
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      03-28-2007
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 ((E-Mail Removed), 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.

 
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 15 03-28-2007 01:15 PM
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