Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

How to create a Visual WebPart using custom user controls in VS2012 for SP2013?

$
0
0

Hi everyone,

I try to create a Visual Webpart for SharePoint 2013 in Visual Studio 2012 and want to add a custom user control to it by referencing its .ascx-file and then embed the tag like it's done in the SP2010-way:

<%@ Register TagPrefix="uc" TagName="example" Src="~/_controltemplates/15/myUserControl.ascx" %>

...

<div>
some content...<uc:example runat="server"/>
more content and some other html...
</div>

In several blogs I read that the new SP2013 Visual WebPart aktually is a new kind of the Sandboxed Visual WebPart. So I get an error that says "The name ‘InitializeControl’ does not exist in the current context" because the autogenerated content is indeed no longer generated as I use the custom user control.

I understand that there is a conflict because the WebPart-Code now is completely compiled and therefor cannot load external .ascx-controls on runtime.

But what's an alternative for this issue? It doesn't seem to be a good style to add every custom usercontrol by adding Page.LoadControl("~/_controltemplates/15/myUserControl.ascx") in CreateChildControls in code-behind if I could add it directly into the html-structure in the .ascx-file of the WebPart.

Do you have a best-practice example for me how to deal with this problem?

Greetings and thanks alot,

Ricky


Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>