Skip to content


How To Add A UserControl To A Web Part In SharePoint 2007

Creating webparts has some drawbacks when it comes to developing them. Just the fact alone that you can’t visually see them while you work on them can be frustrating. User Controls are easier in that way. This is a guide to how you can create a Web Part that consists of one or more User Controls.
Get the best of both worlds :)

There is a link to the zipped solution in the bottom of the post.

Start with a blank Web Application.

1: Create the folders for SharePoint.

“TEMPLATE” in root of the project, “CONTROLTEMPLATES” inside that one, and finally the subfolder for your User Control(s). I named mine “Code-Journey”.

2: Add User Control

If you have created the project as a Class Library project you can’t do this in Visual Studio 2008. User Controls can’t be placed in subfolders in VS2008 using this template. No idea why not.
You can see how you can change a project from a class library to a web project here

Add a User Control to the project in the “Code-Journey” folder and user this markup for it.

1
2
<%@ Control Language="C#" AutoEventWireup="true"  CompilationMode="Always" Inherits="UserControlInWebPart.Code_JourneyUC,Code-Journey.UserControlInWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=efdef2519d9fa74c" %>
3
<asp:Label ID="Label1" runat="server" Text="Name: "></asp:Label>
4
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
5
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /><br />
6
<asp:Label ID="Label2" runat="server"></asp:Label>

Note here that I am using codebehind for my User Control.

inherits=”namespace.classnameForYourCodeBehind, assemblyname,version, culture, publickeytoken”

3: Add Web Part to contain the User Control

01
using System;
02
using System.Data;
03
using System.Configuration;
04
using System.Web;
05
using System.Web.Security;
06
using System.Web.UI;
07
using System.Web.UI.HtmlControls;
08
using System.Web.UI.WebControls;
09
using System.Web.UI.WebControls.WebParts;
10
 
11
namespace UserControlInWebPart
12
{
13
  public partial class UserControlInWebPart : WebPart
14
  {
15
    protected Control myUserControl;
16
 
17
    protected override void CreateChildControls()
18
    {
19
      base.CreateChildControls();
20
      string pathToUserControl = @"/_controltemplates/Code-Journey/Code-JourneyUC.ascx";
21
      myUserControl = Page.LoadControl(pathToUserControl);
22
      Controls.Add(myUserControl);
23
    }
24
  }
25
}

We dynamically load the user control in the webpart, and voilá we have a webpart that consists of a usercontrol or more than one if we wish.

4: Sign assembly by right clicking the project in the solution explorer and selecting “Properties”. Click “Signing” in the left tabs, and “” in the drop down list. Uncheck the checkbox to avoid a password.

5: Create batch deployment script and run it as a postbuild command.

6: Build and let the script copy to gac.

7: Add the assembly for the webpart to safecontrols. The User controls are all added to safecontrols by default in the web.config by sharepoint.

8: Go to the webpart gallery and click “New”, choose your webpart and click “Populate gallery”

9: Add webpart to a webpart page by editing the page and clicking one of the yellow links to add the webpart.

Download the zipped solution below:
http://code-journey.com/wp-content/uploads/2009/07/usercontrolinwebpart.zip

(Download plugin apparently not working)

DeliciousDiggTwitterFacebookLinkedInTechNetStumbleUponNewsVineTechnorati FavoritesSlashdotMSDNShare

Related posts:

  1. Hide the QuickLaunch in SharePoint 2010 (and 2007)
  2. How to set SharePoint 2007 database to read-only
  3. Open links in a new window – Sharepoint 2007 and 2010
  4. Upload Multiple items disabled – SharePoint 2007 and 2010
  5. Create Simple SharePoint Hello World WebPart And Add It To a Web Part Page

Posted in SharePoint 2007.


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Zullu says

    Hi Thomas,
    I had the same requirements as demonstrated in your example above. I was very excited that I got to see your blog and immediately jumped implementing it in VS 2008 and MOSS.
    The download never worked, so I went ahead and typed all (not too much). Now when I go ahead and deploy my web part it doesn’t display anything other the the web part header.
    When I tried to debug, I found that it fails on line number 21. this is the error that I get:
    “The file /_CONTROLTEMPLATES/Code-Journey/Code_JourneyUC.ascx does not exist.”

    Just curious, what is the underscore doing in the line 20 where we declare the path to ascx. I tried with/without it but no use.
    Did try to raise the trust level to “Full’, still no use.
    Do you have any insight to this.
    Any help would be appreciated.
    Thanks
    Zullu.

    • Code-Journey says

      Hi Zullu

      Well sorry for the download trouble. I was not aware it wasn’t working. Apparently my whole download manager is broken.
      I have very little time for the blog right now I’m afraid, but I have added a link in the bottom of the post. It should work.

      It looks like the Code_JourneyUC.ascx is simply using the wrong filename. It not an underscore for a hyphen “-”.
      The filename for the user control is Code-JourneyUC.ascx.

      I hope this and the file works for you, unless you already solves it.

      Best Regards
      Thomas Lund

  2. Zullu says

    Thomas,
    Thanks a bunch !
    I was able to download the project now. And it works !!
    Awesome, you are the man ! :-)
    I must have been typing something wrong. I’ll figure that out later.
    But for now, I am good. I am able to see the user control displayed on the page.
    Thanks again.
    Zullu.

    • Code-Journey says

      Hi Zullu

      No problem. Glad I could help.

      Best Regards,
      Thomas Lund



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin