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 UserControlInWebPart12 {13 public partial class UserControlInWebPart : WebPart14 {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 “
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)
Related posts:
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.
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
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.
Hi Zullu
No problem. Glad I could help.
Best Regards,
Thomas Lund