Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

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 on Wednesday, August 16, 2006 1:56 PM ASP.NET | Back to top


Comments on this post: Dynamically add User Control to other ASP.NET controls children list.

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting Gravatar...
Great stuff, useful tip!
Left by Velislav Gebrev on Sep 19, 2006 6:26 PM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting 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?
Left by Guillaume on Oct 30, 2007 10:08 PM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting 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
Left by Michael Freidgeim on Oct 31, 2007 12:11 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting Gravatar...
Thanks!
.
Left by abs on Nov 09, 2007 7:17 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting Gravatar...
Nice post
Thanks for the Article
Left by Venkat on Jul 24, 2008 6:14 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting Gravatar...
Thank you, that was really, really helpful.

Cheers

Dan
Left by Dan Towers on Jan 29, 2009 3:32 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting 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
Left by web development company on Aug 28, 2009 8:23 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting 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.
Left by xr280xr on Oct 13, 2009 6:09 PM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting 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 ?
Left by Marc on Feb 08, 2010 8:35 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting Gravatar...
Great article - you really saved me some time with this ;-@
Left by Estate Agency Software Expert on Nov 23, 2010 4:31 AM

# re: Dynamically add User Control to other ASP.NET controls children list.
Requesting Gravatar...
Fantastic article - keep up the good work!
Left by Estate Agency Software AgentPro on Aug 31, 2011 3:41 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net