473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
Mar 25 '07 #1
15 1842
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:eD**************@TK2MSFTNGP04.phx.gbl...
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...
Mar 25 '07 #2
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" <ma**@markNOSPAMrae.comwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:eD**************@TK2MSFTNGP04.phx.gbl...
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...


Mar 25 '07 #3
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...
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...
Mar 25 '07 #4
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" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...
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...


Mar 25 '07 #5
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:e5**************@TK2MSFTNGP04.phx.gbl...
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...
Mar 25 '07 #6
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" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:e5**************@TK2MSFTNGP04.phx.gbl...
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...


Mar 26 '07 #7
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 (wa****@online.microsoft.com, 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.


Mar 26 '07 #8
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>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...
Mar 26 '07 #9
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" <ma**@markNOSPAMrae.comwrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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...



Mar 26 '07 #10
Howdy,

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
Mar 28 '07 #11
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!
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:4D**********************************@microsof t.com...
Howdy,

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


Mar 28 '07 #12
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 (wa****@online.microsoft.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.

Mar 28 '07 #13
Thank you for your detailed answer!

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:ez**************@TK2MSFTNGHUB02.phx.gbl...
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 (wa****@online.microsoft.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.
>

Mar 28 '07 #14
Hello Mark,

A new thread has actually made it. The solution is to load the control by
LoadControl rather by SomeCtl ctl = new SomeCtl().
Thanks anyway!!

Ofer
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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...


Mar 28 '07 #15
"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
A new thread has actually made it. The solution is to load the control by
LoadControl rather by SomeCtl ctl = new SomeCtl().
Excellent!
Mar 28 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
6
by: Louise | last post by:
I use Visual Basic .NET Framework 1.1, Visual Studio. I have a Web page (aspx) referencing a control (ascx) that has a placeholder that is assigned one of 2 child controls through buttons. The...
2
by: JohnR | last post by:
Let's say I have an MDI parent form with a textbox. If I create an MDI child form and, at runtime, move the MDI child window over the textbox on the MDI parent, the textbox appears in front of the...
1
by: Tony Johansson | last post by:
Hello!! I use VS 2003 and C#. I have sent several mail even tried with crossgroup because I want to find out if my problem is a bug in .NET or if .NET doesn't support what I do. It's only one...
1
by: Tony Johansson | last post by:
Hello!! I use VS 2003 and C#. I have sent several mail even tried with crossgroup because I want to find out if my problem is a bug in .NET or if .NET doesn't support what I do. It's only one...
3
by: Mike Collins | last post by:
I have some controls that I am creating dynamically. After the user finishes entering their answers in these controls, I would like to iterate through these controls and get the answers out. I have...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
3
by: Ofer Zelig | last post by:
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: ...
6
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Tried posting this on Build Controls but then saw that's a pretty slow group... Kind of a typical request from product management - they want to be able to swap in different 3rd party...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.