5,693,062 members and growing! (17,188 online)
Email Password   helpLost your password?
Enterprise Systems » SharePoint Server » Web Parts     Beginner License: The Code Project Open License (CPOL)

Simple WebPart To Load UserControl

By agusto xaverius

It is a simple webpart but very usefull to load many usercontrols
C# (C# 1.0, C# 2.0, C# 3.0, C#), VB 9.0, VB, Windows (Windows, Win2003), ASP.NET, Dev

Posted: 12 Sep 2008
Updated: 12 Sep 2008
Views: 3,271
Bookmarked: 11 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 1.20 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 50.0%
3
0 votes, 0.0%
4
1 vote, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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.

Background

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()
        { }

        [
          Personalizable(),
          Category("Miscellaneous"),
          DefaultValue(""),
          WebBrowsable(true),
          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)
               RenderChildren(output);
        }

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

        protected override void CreateChildControls()
        {
            base.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=1.0.0.0, 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.

License

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

About the Author

agusto xaverius


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
http://geeks.netindonesia.net/blogs/agusto
Occupation: Architect
Company: Plasmedia
Location: Indonesia Indonesia

Other popular SharePoint Server articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralError while adding the user controlmemberjgeeetha0:55 20 Nov '08  
GeneralRe: Error while adding the user controlmemberagusto xaverius3:44 22 Nov '08  
Generalreferenced dllsmemberseanrogers8:44 30 Oct '08  
GeneralRe: referenced dllsmemberagusto xaverius7:30 10 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Sep 2008
Editor: Sean Ewington
Copyright 2008 by agusto xaverius
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project