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

Can i add a user control to a Visual Web Part (in visual studio) in sharepoint 2010?

I have a user control which has content that i want placed on most of my visual web parts.

Has anyone got any sample code doing this?

share|improve this question
    
Your answer acceptance rate is 0, and that is why most of the people don't answer your questions! –  Arsalan Adam Khatri Nov 9 '12 at 10:32
    
i have accepted answers by ticking the green tick. is this ok now? –  user1786107 Nov 9 '12 at 10:40
    
Good one, now it says 100% accept rate! Thanks fella! –  Arsalan Adam Khatri Nov 9 '12 at 16:28
add comment

2 Answers

up vote 1 down vote accepted

You can follow two methods:

from msdn

  1. In Visual Studio, open or create a SharePoint project.

  2. In Solution Explorer, choose the project node.

  3. On the menu bar, choose Project, Add New Item. The Add New Item dialog box opens.

  4. In the Installed pane, expand the SharePoint node, and then choose the 2010 node.

  5. In the list of SharePoint templates, choose User Control (Farm Solution Only).

  6. In the Name box, specify a name for the user control, and then choose the Add button. Visual Studio adds several folders and files to your project.

    By default, the user control file appears in the Source view of the Visual Web Developer designer. In this view, you can edit the XML markup of the control. You can switch to Design view if you want to design the control visually by dragging controls from the Toolbox.

  7. If you want to handle events that occur in the control, add code to the code file of the user control.

This file appears in Solution Explorer under the user control file and has a .cs or .vb extension, depending on the language of the project.

http://msdn.microsoft.com/en-us/library/ee231548.aspx

in code you can do the following also taken from msdn ;) :

[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/CS/VisualWebPart1/VisualWebPart1UserControl.ascx";

    public VisualWebPart1()
    {
    }

    protected override void CreateChildControls()
    {
        Control control = this.Page.LoadControl(_ascxPath);
        Controls.Add(control);
        base.CreateChildControls();
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        base.RenderContents(writer);
    }

}

Creating Reusable Controls for Web Parts or Application Pages

http://msdn.microsoft.com/en-us/library/ee231577.aspx

I think this is the one your looking for (url above :) with the code above explained )

or

http://thesoftwarecondition.com/blog/2012/02/22/how-to-add-a-user-control-to-a-sharepoint-web-part/

hope it helps :)

share|improve this answer
add comment

I add a custom message control to almost every webpart I develop.
I put a PlacerHolder control on visual webpart and on page PreRender event I add that message control to PlaceHolder

<asp:PlaceHolder ID="phMsg" runat="server"/>

Page PreRender Event

protected void Page_PreRender(object sender, EventArgs e)
{
    // Add messages to page
    phMsg.Controls.Add(msg.MsgList);
}
share|improve this answer
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.