how to load usercontrols from the assembly using the filename?

1.3k Views Asked by At

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

1

There are 1 best solutions below

2
On BEST ANSWER

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.