all groups > asp.net building controls > june 2005 >
You're in the

asp.net building controls

group:

dynamically add usercontrol


dynamically add usercontrol Snorre
6/14/2005 3:39:02 AM
asp.net building controls: Hi
Can anybody explain to me how to dynamically add a usercontrol? When I try
to use the this.Controls.Add the control is added, but none of the
usercontrol member controls are instanciated.

The workaround I have done is to add the control to the aspx and set visible
to false. Not very elegant, so I would really like to add the control from
the codebehind file instead.

Re: dynamically add usercontrol Matt
6/14/2005 5:55:29 AM
Are you loading it before you add it?
Re: dynamically add usercontrol Snorre
6/14/2005 6:06:03 AM
I'm doing now :) thanks.

I find it strange though that I have to refer to the ascx file to make the
page aware of the class (through LoadControl). Do you know of a more
sophisticated way to do this?



[quoted text, click to view]
Re: dynamically add usercontrol Matt
6/14/2005 7:20:42 AM
The ascx file is parsed at runtime ... I think it has weird
auto-generated class names like aspx files do ... you could write
custom controls, but then you have to write your own designer classes
.... In my situation, I have a default page with a place holder into
which I dynamically load user controls ... I have a sitemap.xml
document with entries for each user control containing the path, title,
name etc. ... when I want to load a user control I look up the path in
the xml document ... that might give you a little more separation
between your code and physical path to the user controls ... if that's
what you're looking for
Re: dynamically add usercontrol Ron
2/24/2006 7:01:26 AM
Hi,
I am trying to accomplish this same task would i really appreciate some code
examples if possible.


[quoted text, click to view]
AddThis Social Bookmark Button