I've got a visual web part (created with standart Visual Stuido 2012 template from "Add new item" form) with just a single <div id="newsListDiv" runat="server"></div>
element. I want to programmatically add my own user control to it using the following code:
protected void Page_Load(object sender, EventArgs e)
{
NewsLine newsLine = Page.LoadControl(@"~/_ControlTemplates/MainTheme/NewsLine.ascx") as NewsLine;
newsListDiv.Controls.Add(newsLine);
}
But when I deploy the solution and add the web part to the page it shows an error page, telling me that the file '/_ControlTemplates/MainTheme/NewsLine.ascx' does not exist. But if I look into folder "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\CONTROLTEMPLATES\MainTheme" I can see that the file is present there. I've tried to set the trust level to "Full" but got the same error. I also tried adding the user control in .ascx file like this:
<%@ Register Src="~/_controltemplates/MainTheme/NewsLine.ascx" TagPrefix="uc1" TagName="NewsLine" %>
<div id="newsListDiv" runat="server">
<uc1:NewsLine runat="server" id="NewsLine" />
</div>
And that way a get a compilation error: "The name 'InitializeControl' does not exist in the current context". I've also noticed, that as soon as I add Register or Reference line (with the path to my user control) to my .ascx file, the .g.cs file becomes blank! And it fills up again when I remove that line. I tried many different path writings like "../_controltemplates/", "/controltemplates/15/", etc. But none of them made any difference. I'm getting desperate here, please help!
You forgot to try one more option. It's the same when accessing the _layouts folder. You should specify the 15 hive.
The correct path is "~/_ControlTemplates/15
NewsLine newsLine = Page.LoadControl(@"~/_ControlTemplates/15/MainTheme/NewsLine.ascx")