469,926 Members | 1,940 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

User Web Control Load Problem

I have built a composite user web control that I want to create dynamically.
The form will contain a variable number of these controls and as well some
of the contents of the user web control itself are dynamically created
controls.

I create as follows:
wcGroupControl oGroupControl = new wcGroupControl();

I then tried calling methods of the control passing dynamic controls as
parameters that need to be added to the web controls on the control. Well I
quickly realized that the controls within the UWC are not created until the
control is actually rendered on a form, doh!.

Is there a way around my delema? Should I create the controls contained
within UWC in the constructor instead?
Nov 18 '05 #1
3 2104
Hi Eric,
Thanks for posting in the community!
From your description, you've developed a custom web server control which
is used in some certain web pages( static or dynamically added). Also, the
control is a composite control and it has a member function which takes a
control parameter so as to let it add some dynamically created sub
controls. However, you found that the custom server control's controls
structure may haven't been completed when the certain add control function
is called to add sub control, that'll cause some problem at runtime, so you
wonder whether there is any way to ensure the controls strucutre has been
completely constructed so that we can call its function to add dynamic sub
controls correctly, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, here is my suggestions:
Yes, as you've found that when the server control has completely
constructed its controls struture is unexpected at runtime. Generally, the
server control will call createchildcontrol method to construct its
controls collection after the "Init" event, however, this all depends on
the lifecycle of its container page since a control reside on a certain
page , do you think so? So if we directly access a certain server control's
control collection and added some dynamically created sub controls(in
Page's OnInit or Page_Load event) , it will cause some problem since the
entire control strucutre of the server control hasn't been constructed.
However, there is a member function named "EnsureChildControls" of
System.Web.UI.Control which is used to ensure that the control's controls
structure has been completely constructed. Here is the description in MSDN:
----------------------------
This method first checks the current value of the ChildControlsCreated
property. If this value is false, the CreateChildControls method is called.

ASP.NET calls it this method when it needs to make sure that child controls
have been created. In most cases, custom server control developers do not
need to override this method. If you do override this method, use it in a
similar fashion as its default behavior.
-----------------------------
For more detailed info on it, you may view the following references in MSDN:
#Control.EnsureChildControls Method
http://msdn.microsoft.com/library/en...webuicontrolcl
assensurechildcontrolstopic.asp?frame=true

#Control.ChildControlsCreated Property
http://msdn.microsoft.com/library/en...WebUIControlCl
assChildControlsCreatedTopic.asp?frame=true

So as for your situation, you may call the custom server control's
ensurechildcontrols method before you call the add dynamic control method
so that the dynamically created control can be correctly added into the
control's Controls structure, how do you think of this?

In addition here are some other tech articles on developing Custom
Composite server control and dynamically add controls on ASP.NET page, I
believe they're also helpful to you:

#Developing a Composite Control
http://msdn.microsoft.com/library/en...lopingcomposit
econtrols.asp?frame=true

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/?id=317794

Please check out the preceding suggestions and items. If you have any
questions on them, please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 18 '05 #2
Steven,
Thank you for the reply. See notes below.
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:3R**************@cpmsftngxa07.phx.gbl...
Hi Eric,
Thanks for posting in the community!
From your description, you've developed a custom web server control which
is used in some certain web pages( static or dynamically added). Also, the
control is a composite control and it has a member function which takes a
control parameter so as to let it add some dynamically created sub
controls. However, you found that the custom server control's controls
structure may haven't been completed when the certain add control function
is called to add sub control, that'll cause some problem at runtime, so you wonder whether there is any way to ensure the controls strucutre has been
completely constructed so that we can call its function to add dynamic sub
controls correctly, yes?
Exactly.
If there is anything I misunderstood, please feel free to let me know.

As for this problem, here is my suggestions:
Yes, as you've found that when the server control has completely
constructed its controls struture is unexpected at runtime. Generally, the
server control will call createchildcontrol method to construct its
controls collection after the "Init" event, however, this all depends on
the lifecycle of its container page since a control reside on a certain
page , do you think so? So if we directly access a certain server control's control collection and added some dynamically created sub controls(in
Page's OnInit or Page_Load event) , it will cause some problem since the
entire control strucutre of the server control hasn't been constructed.
However, there is a member function named "EnsureChildControls" of
System.Web.UI.Control which is used to ensure that the control's controls
structure has been completely constructed. Here is the description in MSDN: ----------------------------
This method first checks the current value of the ChildControlsCreated
property. If this value is false, the CreateChildControls method is called. Sounds exactly what I need. I will try this.
Thank you ASP.NET calls it this method when it needs to make sure that child controls have been created. In most cases, custom server control developers do not
need to override this method. If you do override this method, use it in a
similar fashion as its default behavior.
-----------------------------
For more detailed info on it, you may view the following references in MSDN: #Control.EnsureChildControls Method
http://msdn.microsoft.com/library/en...webuicontrolcl assensurechildcontrolstopic.asp?frame=true

#Control.ChildControlsCreated Property
http://msdn.microsoft.com/library/en...WebUIControlCl assChildControlsCreatedTopic.asp?frame=true

So as for your situation, you may call the custom server control's
ensurechildcontrols method before you call the add dynamic control method
so that the dynamically created control can be correctly added into the
control's Controls structure, how do you think of this?

In addition here are some other tech articles on developing Custom
Composite server control and dynamically add controls on ASP.NET page, I
believe they're also helpful to you:

#Developing a Composite Control
http://msdn.microsoft.com/library/en...lopingcomposit econtrols.asp?frame=true

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/?id=317794

Please check out the preceding suggestions and items. If you have any
questions on them, please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Nov 18 '05 #3
Hi Eric,
Thanks for your followup. Hope that'll help you resolve your problem.
Meanwhile, if you meet any further problems or need any help, please always
feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by martin | last post: by
1 post views Thread by Kris van der Mast | last post: by
3 posts views Thread by Matej Kavčič | last post: by
2 posts views Thread by Sam Kuehn | last post: by
8 posts views Thread by David Lozzi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.