I have a Webpart with a "main class" named myClass

public class myClass : Microsoft.SharePoint.WebPartPages.WebPart

and I want to load a ascx file from this class like this


but when I trying to do this:

 Control c = Page.LoadControl("~/myAscx.ascx");


 Control c = Page.LoadControl("~/nameWebpart/myAscx.ascx");

I only get the message:

"The file myAscx.ascx does not exist"

The file myAscx is in the same "folder" of the myClass. How can I know the correct path?


You can place Your ascx in the layouts folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ and then

MyClass c = (MyClass)Page.LoadControl("/_layouts/MyAscx.ascx");

Of course You can also create a subfolder under the LAYOUTS directory and update the LoadControl path accordingly.

| improve this answer | |
  • It works fine ^^ – user674887 Aug 23 '11 at 11:58

ascx files are stored in the COntrolTemplates folder in the 14 hive of sharepoint.

So it's path will be something like:

private const string _ascxPath = @"~/_CONTROLTEMPLATES/ProjectFolder/controlname.ascx";
| improve this answer | |
  • Thanks, but this was a custom ascx that places in another folder. Maybe the controltemplates is only for default ascx – user674887 Aug 23 '11 at 11:57
  • 1
    the _controltemplates can be substituted with whatever location under the 14 hive you want (_layouts, _vti_bin (ISAPI) etc. etc.) – Colin Aug 24 '11 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.