Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

http://www.mojoportal.com/Forums/Thread.aspx?pageid=5&mid=34&ItemID=9&thread=3051&pagenumber=1

but when I trying to do this:

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

or

 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?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

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

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";
share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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