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). 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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.

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

Left by Marc at 2/8/2010 8:35 AM
Gravatar
Same here. I used the LoadControl method. I made an overloaded constructor with 2 paramaters. Trying to dynamically load it gave null for the childcontrols, while loading with the default constructor went perfect. No clue why, coz I supplied the parameters in the LoadControl() method, and every line of code was touched and gave no errors. Just the fact that the childcontrols all were null.

Someone a clue what can be the case here ?

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

Left by Estate Agency Software Expert at 11/23/2010 4:31 AM
Gravatar
Great article - you really saved me some time with this ;-@

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

Left by Estate Agency Software AgentPro at 8/31/2011 3:41 AM
Gravatar
Fantastic article - keep up the good work!

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910