LoadControl() method throws virtual path error when attempting to load user control from subfolder.

J

Jeremy

Everything in my app worked fine - then I moved some user controls to a
subfolder named \ControlTemplates that physically exists directly under the
site root.

Then, at runtime when these lines attempts to load a user control from that
subdirectory:
string templateFileName = @"/ControlTemplates/Template01.ascx";
Control templateControl = LoadControl(templateFileName);

The application throws an error with the following message:
The virtual path '/ControlTemplates/Template01.ascx' maps to another
application, which is not allowed.

How can I load user controls that are physically stored in a subfolder under
the site root?

Thanks.
 
O

Oleg Ogurok

Try adding a tilda (~)
string templateFileName = @"~/ControlTemplates/Template01.ascx";

or you can get the root of your app this way:

string templateFileName = Request.ApplicationRoot +
@"/ControlTemplates/Template01.ascx";

-Oleg.
 
J

Jeremy

Thank you - that solved the problem.


Oleg Ogurok said:
Try adding a tilda (~)
string templateFileName = @"~/ControlTemplates/Template01.ascx";

or you can get the root of your app this way:

string templateFileName = Request.ApplicationRoot +
@"/ControlTemplates/Template01.ascx";

-Oleg.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top