Building my own UserControl WebPart part IV

In this fourth article about my own UserControl WebPart I will explain how a WebPart can use its own editor parts. Editor parts are very handy when it comes to customizing / personalizing and categorizing the WebPart. In this blog I will only show you how to add an editor part and how to use it to display some common info about the WebPart.

When implementing a editor part you have to do the following steps.

Step 1 Create a class which inherits from EditorPart

namespace My.WebParts.Editors
{
    public class AboutEditor : System.Web.UI.WebControls.WebParts.EditorPart
    {
 
    }
}

Step 2 Implements its methods

namespace My.WebParts.Editors
{
    public class AboutEditor : System.Web.UI.WebControls.WebParts.EditorPart
    {
        public override bool ApplyChanges()
        {
            return true;
        }
 
        public override void SyncChanges()
        {
        }
 
    }
}

Since I don't use the editor part to edit my WebPart I'm not obligated to add any code to the ApplyChanges (Saves the values in an EditorPart control to the corresponding properties in the associated WebPart control.) and SyncChanges (Retrieves the property values from a WebPart control for its associated EditorPart control.) methods.

Step 3 Create a useful editor part

using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
 
namespace My.WebParts.Editors
{
    public class AboutEditor : EditorPart
    {
        public AboutEditor()
        {
            this.ID = "AboutEditor";
            this.Title = "About";
        }
 
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.WriteLine("UserControl WebPart");
            writer.WriteBreak();
            writer.WriteLine("Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString());
            writer.WriteBreak();
            writer.WriteBreak();
 
            writer.WriteLine("Created by: ");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "http://bloggingabout.net/blogs/mglaser");
            writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("Mike Glaser");
            writer.RenderEndTag();
            writer.WriteBreak();
            writer.WriteBreak();
 
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "http://www.class-a.nl");
            writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("www.class-a.nl");
            writer.RenderEndTag();
        }
 
        public override bool ApplyChanges()
        {
            return true;
        }
 
        public override void SyncChanges()
        {
        }
 
    }
}

Step 4 Extend your WebPart class to implement the IWebEditable interface

public class UserControlWebPart : WebPart, IWebEditable

Step 5 Implement the CreateEditorParts method and use the editor part

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
 
using My.WebParts.Editors;
 
namespace My.WebParts
{
    public class UserControlWebPart : WebPart, IWebEditable
    {
        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();
            }
        }
 
        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> editors = new List<EditorPart>();
            editors.Add(new AboutEditor());
            return new EditorPartCollection(editors);
        }
 
   }
}

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, I now have create my first editor part.

Summary
I only showed how to use an editor part, but I didn't show how to customize / personalize a WebPart using this editor part. In the next parts I will create the two other editor parts. One article will be about selecting the user control, the other about displaying properties of it.

2 thoughts on “Building my own UserControl WebPart part IV

  1. anonymous

    i cannot download the file attachment, it says that the file was unknown or damaged...

    can you provide another copy for download?
    just want to study the codes...

    thanks...i found your article very useful...

    Reply

Leave a Reply

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