Template.LoadControl(type,object[])...doesn't work as expected

K

Keith Patrick

Maybe my expectations are just too low because I haven't read much
documentation on this method, but basically, I am trying to load a user
control dynamically without using a virtual path (I have disabled
FileIOPermission beyond AllLocalFiles, and it appears that
LoadControl(string) calls Server.MapPath, which needs greater access). What
I am attempting to do is just pass in my UC type:
Control ctrl = this.Page.LoadControl(TestApp.Controls.MyUserControl, null);

but when I do this, the control doesn't show up. I've looked at the
differences between controls loaded this way and via virtual path, as the
virtual path version displays just fine. The underlying types, however are
different. The virtualpath one is of type "ASP.MyUserControl_aspx" whereas
the one loaded from type is TestApp.Controls.MyUserControl and also lacks
TemplateXXXX properties. Am I somehow misuing LoadControl(type, object[])
or is there a bug that prevents dynamic control loading like this?
 
B

Bruce Barker

you are loading the codebehind class, so if the code behind depends on
anything in the aspx file, it won't work. the loadcontrol from virtual path
will compile the aspx page if needed, and uses its class, not the codebehind
class.

you should switch to server (code only) controls if you want to use this
technique.

-- bruce (sqlwork.com)
 
K

Keith Patrick

I knew it worked that way in 1.1, but I would think that since 2.0
implements them as partial classes, they would be linked together at compile
time such that my namespace-qualified classname also corresponds to the
full, non-partial class, since I can go into TestApp.Controls.MyUserControl
and reference controls that have only been declared in the codebehind at
design-time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top