Building my own UserControl WebPart part III

Since my colleagues Dennis and Alex are writing articles by the minute, it was time for me to do the same. Dennis is writing some very interesting articles about WCF (Windows Communication Foundation) and Alex is doing the same about Unit Testing in Visual Studio .NET 2005.

Unfortunately I wasn't able to write about my own UserControl WebPart for a long time. One thing was, I had to so much other stuff (training, consult, family & friends), it didn't give me much spare time. An other thing was, I wasn't able to copy my code formatted, as I like it, from my SharePoint development environment running on a VPC. I had to spend some time on this problem too.

In this third part I'm going to write about creating an ASP.NET 2.0 WebPart which dynamically loads a UserControl and can be used in a SharePoint or an ASP.NET 2.0 Portal. Before I start I will mention I haven't coded any .NET language for over a year now. So if you have any improvements or comments, please let me know. As I described in my first article about WebParts, to use an ASP.NET 2.0 WebPart in SharePoint you don't have to do a lot of things. One thing which is really important is to make sure you will inherit from the correct WebPart.

using System;
 
namespace Sample.TestWebParts
{
    public class Class1 : System.Web.UI.WebControls.WebParts.WebPart
    {
    }
}

When doing so you could stop here, but it is nice when your WebPart becomes rendered it shows some information. In this example I'll show code which displays 'Superb' as a text when the WebPart hits the page.

using System;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
namespace Sample.TestWebParts
{
    public class Class1 : WebPart
    {
        protected override void CreateChildControls()
        {
            Label control = new Label();
            control.Text = "Superb";
 
            this.Controls.Add(control);
        }
 
    }
}

or

using System;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
 
namespace Sample.TestWebParts
{
    public class Class1 : WebPart
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("Superb");
        }
    }
}

To know how you should create a good WebPart you must be very confident with the Web load- and event handlers model. Because this is hard to explain, I'm not discussing these principles here. I hope you understand this is the reason why I want to build my own UserControl WebPart. With this WebPart you can concentrate on business problems instead of technical problems. In mine last article I explained how I like to use a UserControl and where it should be stored. So for displaying it dynamically you can use this code.

using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
 
namespace My.WebParts
{
    public class UserControlWebPart : WebPart
    {
        public UserControlWebPart()
        {
            this._userControlPath = "/UserControls";
        }
 
        private UserControl _control;
        private string _userControlPath;
        private string _userControl = string.Empty;
 
        [WebBrowsable(false), DefaultValue("/UserControls")]
        public string UserControlPath
        {
            get
            {
                return this._userControlPath;
            }
            set
            {
                this._userControlPath = value;
            }
        }
 
        [WebBrowsable(true), Personalizable(true)]
        public string UserControl
        {
            get
            {
                return this._userControl;
            }
            set
            {
                this._userControl = value;
            }
        }        
 
        protected internal void LoadUserControl()
        {
            if (!string.IsNullOrEmpty(this._userControl))
            {
                base.CreateChildControls();
                this.Controls.Clear();
                this._control = (UserControl)Page.LoadControl(this._userControlPath + "/" + this._userControl);
 
                this.Controls.Add(this._control);
            }
        }
 
        protected override void CreateChildControls()
        {
            try
            {
                LoadUserControl();
            }
 
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
 
   }
}

When using this WebPart in my test portal it will give the image below.


UserControl in ASP.NET 2.0 Portal using my own UserControl WebPart

As you can see in the image, all properties which where supplied by the UserControl are disappeared. I have to write my own editor part, which supply the properties by reflection.

Summary
I enjoyed writing some extra info about my own UserControl WebPart. In the next part I will explain how to use editor parts and how they fill into my WebPart.

Leave a Reply

Your email address will not be published. Required fields are marked *