Add user control to web part in Sharepoint 2013
Asked Answered
P

3

13

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!

Pasteurization answered 10/1, 2013 at 10:8 Comment(0)
H
19

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") 
Holyhead answered 29/1, 2013 at 10:46 Comment(2)
Tried that one too, still no luck. It seems to me that there's some problem with the tool that automatically generate .g.cs file. Tried to re-install VS, but that didn't help either. I decided not to use UserControls for now, but I got an idea that it might not work because of I created ControlTemplates folder in the VS not as a mapped folder. Gonna check on that later...Pasteurization
this helped me for 2010->2013 upgrade on "bla-bla.ascx" file was not found issueIsometry
F
0

Was running into the same issue. I had not seen the suggestion for adding the 15 to the path and this worked for me. The only other difference between my reference and yours is I do not have the ~ at the start. The / is the first character.

Fibrovascular answered 26/3, 2013 at 1:20 Comment(0)
A
0

I have this same problem only it works when I do it in code behind but I get no luck tring to add it to ascx like any other controls I use! - God Why should everything be this different in sharepoint?!!!!

Auspicate answered 14/6, 2014 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.