views:

158

answers:

1

we have a set of usercontrols complied to a dll. we could just load the controls using their classnames by like ASP.theusercontrol_ascx blah = new ASP.theusercontrol_ascx();

but if we have dynamic data and load the controls based on the filename (or the control name), how can we load them like Page.LoadControl("TheURL/theusercontrol.ascx")?

Thanks a lot...

+2  A: 

There isn't really a proper way of doing this. .ascx files are not meant to be deployable this way. The code has been compiled to the .dll, but the actual markup from the .ascx file has not. Even if you follow the official Microsoft documentation on converting .ascx files to deployable controls, you lose the ability to call LoadControl based on file name, simply because there is no markup file anymore.

There are a number of hacks floating around that let you do it via reflection and embedding the .ascx files as resources.

Aside from that, if you really need to use LoadControl(filename), you will need to copy over the .ascx files.

womp
If this is the case, we could just instantiate the user control. But the problem is we really need to use strings for the instantiation... And here goes reflection again... =(
Jronny
Oh, well, I really have no choice. Reflection saves my life again. Thanks.
Jronny