Dynamically add User Control to other ASP.NET controls children list.

I wanted to dynamically add my user control as a child to another control.
I've used the code with new constructor, as I did for standard ASP.NET server controls and custom controls.

 Dim ctrlInstances As SearchAreasInstancesUsc = new SearchAreasInstancesUsc() 'Incorrect
tCell.Controls.Add(ctrlInstances)

Then I found that declaratively defined( in ASCX markup) controls are not initialized and set to null.
Of course, I should LoadControl to allow ASP.NET to initialize all declarations.

Dim ctrlInstances As SearchAreasInstancesUsc = LoadControl("SearchAreaInstancesUsc.ascx"'correct
tCell.Controls.Add(ctrlInstances)

UPDATE: I strongly recommend to read a great atricle: TRULY Understanding Dynamic Controls  -Parts 1-4.
(See also related Rendering ASP.NET Controls out of order  and Rendering ASP.NET Controls out of place)

If you still need to use dynamic controls, some more examples are shown in Dynamically Created Controls: Dynamic Rows Sample (and Dynamically Created Controls in ASP.NET). 

posted @ Wednesday, August 16, 2006 1:56 PM

Print

Comments on this entry:

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by Velislav Gebrev at 9/19/2006 6:26 PM
Gravatar
Great stuff, useful tip!

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by Guillaume at 10/30/2007 10:08 PM
Gravatar
I have the same problem than you had, but i've made the LoadControl before.
What is weird is that the control worked fine, and it doesn't work since i haded some UpdatePanel s to limit post back trafic.
Is there any known issues with UpdatePanel and initializing controls?

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by Michael Freidgeim at 10/31/2007 12:11 AM
Gravatar
I am not aware about any particular issues about UpdatePanel and LoadControl, but you should use UpdatePanel with causion. See my "ASP.Net Ajax UpdatePanel links"
http://geekswithblogs.net/mnf/archive/2007/05.aspx

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by abs at 11/9/2007 7:17 AM
Gravatar
Thanks!
.

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by Venkat at 7/24/2008 6:14 AM
Gravatar
Nice post
Thanks for the Article

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by Dan Towers at 1/29/2009 3:32 AM
Gravatar
Thank you, that was really, really helpful.

Cheers

Dan

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by web development company at 8/28/2009 8:23 AM
Gravatar
Humm... interesting,

this workd but how if we want to add cilumn when we push a button every tome on the browser

Keep up the good work

# re: Dynamically add User Control to other ASP.NET controls children list.

Left by xr280xr at 10/13/2009 6:09 PM
Gravatar
I am having what I think is the same problem but I am already using LoadControl().

I am trying to add controls dynamically after my page is initialized (specifically while databinding). I call LoadControl(typeof(MyUserControl), null) to create the control. But when I call a method that sets the text of a Label on my user control, that Label is null.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345