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-us/cpref/html/frlrfsystemwebuicontrolcl
assensurechildcontrolstopic.asp?frame=true
#Control.ChildControlsCreated Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIControlCl
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-us/cpguide/html/cpcondevelopingcomposit
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.)