Click here to Skip to main content
13,141,445 members (24,143 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 12 Sep 2008

Simple WebPart To Load UserControl

, 12 Sep 2008
Rate this:
Please Sign up or sign in to vote.
It is a simple webpart but very usefull to load many usercontrols


Moss/WSS is so CMS popular currently. There are many thing can be which can be done and is modified at deep technology on that product.

Work with on that solution will ever be utilize webpart. But if we develop one webpart sometimes we do ever make same old things which is make coding webpart and load the usercontrol what does we want.


This concept is similiar with SmartPart which you just create one webpart and then it can be used to load all your usercontrol without writing webpart again.

Using the Code

The listing code is like this:

  1. Write on your class solution like this. Please reference Microsoft.Sharepoint.dll component and System.Web.dll too.
using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace WebPartLib
    public class WebPartGeneral : Microsoft.SharePoint.WebPartPages.WebPart
        private Control _childControl = null;
        private string _userControlVirtualPath = string.Empty;
        private string _errMessage = string.Empty;

        public WebPartGeneral()
        { }

          WebDisplayName("User Control Virtual Path"),
          WebDescription("User Control Virtual Path")
        public string UserControlVirtualPath
            get { return _userControlVirtualPath; }
            set { _userControlVirtualPath = value; }

        protected override void RenderWebPart(HtmlTextWriter output)
            if (_errMessage != string.Empty) output.Write(_errMessage);
            if (_userControlVirtualPath != string.Empty ||
                _userControlVirtualPath.Equals("") == false)

        protected override void RenderChildren(HtmlTextWriter output)
                if (this._childControl != null)
            catch (Exception ex)
                _errMessage = string.Format(
                  "Exception Message (RenderWebPart) = {0}<br />", ex.Message);

        protected override void CreateChildControls()
            if (_userControlVirtualPath != string.Empty)
                if (_childControl != null) return;
                _childControl = Page.LoadControl(_userControlVirtualPath);
                if (_childControl != null)
                    Controls.AddAt(0, _childControl);

        public override void Dispose()
  1. After the solution already successfully compile and the put the binary into bin folder on your MOss/Wss site. After it also make one folder by which UserControls.

  1. Modify your web.config
eControl Assembly="WebPartLib, Version=, Culture=neutral"
   Namespace="WebPartLib" TypeName="*" Safe="True"
   AllowRemoteDesigner="True" /> 

and change trust level from WSS_Minimal into Full.

<trust level="Full" originUrl="" /> 
  1. If Necesary you can client restart your IIS and the back to your site again and list the webpart into list of webpart

  2. Create new website solution and create a simple usercontrol and then copy all .ascx and .cs into UserControls folder
  3. Now you can utilize webpart and set the virtual path into your usercontrols and the simsalabim your webpart now load your usercontrol. You can create another usercontrol again and you can add your webpart again and set the usercontrols to load.

Points of Interest

As i mention before, Now every time you create a usercontrol you don't have to create another webpart. It's only use that webpart again and set path into that usercontrols.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

agusto xaverius
Architect Plasmedia
Indonesia Indonesia
I am a developer,software architecture at one's IT's firm at Indonesia. For detail about my profile you can visit my blog at

You may also be interested in...

Comments and Discussions

GeneralCould not load type solved (at least for me) Pin
Clayton Osborn17-Mar-11 2:51
memberClayton Osborn17-Mar-11 2:51 
GeneralGreat Pin
Niccs27-Nov-09 1:16
memberNiccs27-Nov-09 1:16 
GeneralGetting error while loading the user control Pin
am20an22-Oct-09 23:47
memberam20an22-Oct-09 23:47 
QuestionNested LoadControl Pin
guaneme5-May-09 9:01
memberguaneme5-May-09 9:01 
AnswerRe: Nested LoadControl Pin
Gyan Singh27-Aug-09 23:21
memberGyan Singh27-Aug-09 23:21 
GeneralI'm getting error Pin
elnone8520-Apr-09 14:40
memberelnone8520-Apr-09 14:40 
GeneralI have a question please.. Pin
Oslec19-Apr-09 22:33
memberOslec19-Apr-09 22:33 
GeneralWorks Perfectly Pin
Oslec19-Apr-09 21:42
memberOslec19-Apr-09 21:42 
QuestionHow to do step 6 Pin
huela6-Feb-09 11:42
memberhuela6-Feb-09 11:42 
GeneralError while importing .dwp file.. Pin
vimalthehero1-Jan-09 1:20
membervimalthehero1-Jan-09 1:20 
GeneralError while adding the user control Pin
jgeeetha19-Nov-08 23:55
memberjgeeetha19-Nov-08 23:55 
GeneralRe: Error while adding the user control Pin
agusto xaverius22-Nov-08 2:44
memberagusto xaverius22-Nov-08 2:44 
Generalreferenced dlls Pin
seanrogers30-Oct-08 7:44
memberseanrogers30-Oct-08 7:44 
GeneralRe: referenced dlls Pin
agusto xaverius10-Nov-08 6:30
memberagusto xaverius10-Nov-08 6:30 
BugRe: referenced dlls Pin
Muhd Hafiz Ahmad4-Feb-14 18:59
memberMuhd Hafiz Ahmad4-Feb-14 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 12 Sep 2008
Article Copyright 2008 by agusto xaverius
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid