Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Page.LoadControl(_controltemplates) possible?

  Asked By: Jasmin    Date: Dec 26    Category: Sharepoint    Views: 3403

I have a simple User Control Container based on the SmartPart's
technique of dynamically loading the ASCX file. I want to use a file
in 12\LAYOUTS\CONTROLTEMPLATES (for deployment reasons).
Additionally I've got my own codebehind stored in an assembly that I
can put into the <webroot>\bin or the GAC.

I think I'm having problems crossing virtual directory boundaries.
Surprisingly, I can get this to work with Full trust.

// Doesn't seem to matter if starts with "/" or "~/"
string _userControl = @"/_controltemplates/Custom/MyControl.ascx"
if (_userControl != defaultText)
_control = this.Page.LoadControl(_userControl);

I always get this:

at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal
(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp,
Boolean allowBuildInPrecompile)
(HttpContext context, VirtualPath virtualPath, Boolean noBuild,
Boolean allowCrossApp, Boolean allowBuildInPrecompile)
at System.Web.UI.TemplateControl.LoadControl(VirtualPath
at System.Web.UI.TemplateControl.LoadControl(String virtualPath)

Any ideas I can try?



1 Answer Found

Answer #1    Answered By: Walter Stone     Answered On: Dec 26

You posted the stack trace, but I don't see the error message. A few
things to check:

Does the @ control  directive in the user  control markup have the correct
Inherits attribute? It needs the class and assembly. If the assembly  is
in the GAC, be sure to specify the four-part assembly name. I always
specify CompilationMode="Never" when I use a code-behind.

Does the @ Page directive have the correct Inherits attribute? Same
scenario as above.

Is your control variable defined correctly? And, I usually include the
As clause on the statement with LoadControl(). For example:

UserControlType control = this.LoadControl(_userControl) As

Don't forget to add your control to the Controls collection of the
MasterPage placeholder.

Didn't find what you were looking for? Find more on Page.LoadControl(_controltemplates) possible? Or get search suggestion and latest updates.