ASP.NET 2.0

13 Comments

Since the time Jan beat me by delivering his Return of the SmartPart, I still get lots of request to complete my initial blog about dynamically loaded User Controls in SharePoint 2007 / ASP.NET 2.0 Portals. Keep in mind SmartPart is a great WebPart, but focuses completely on SharePoint. The WebPart even inherits from Microsoft.SharePoint.WebPartPages.WebPart and isn't what I found able to use enumerators. Besides this I like to complete where I once started and Microsoft is still not delivering an additional component which they once promised. See a blog of Scott Guthrie where he describes some info about whether it will be possible to host Web Parts built as ASP.NET 2.0 User Controls within SharePoint 2007.

The last time I stuck at part V which was al about the WebPart EditorPart, to select a UserControl from the "UserControls" directory. This part of the blog series focuses on discovering the properties of selected UserControl. In this blog I won't be bother you with big code screen but only with three small ones. All other code will be included as attachment, so you can read and investigate it. You can learn a lot of debugging code. Most of the ASP.NET 2.0 portals uses personalization, which can be setup with the aspnet_regsql.exe command. This command can be found at %WINDOWS%Microsoft.NETFrameworkv2.0.50727. After running this all portal users are able to store their settings of the given WebParts. I added the ability to store the properties of UserControl as well. To set and show the properties of a UserControl you can use the [WebBrowsable] and [Personalizable] attribute.

I divided the properties in three categories.

  1. String -> rendered as a TextBox
    private string _textField;
    [WebBrowsable(true), Personalizable(true)]
    public string TextField
    {
        get
        {
            return this._textField;
        }
        set
        {
            this._textField = value;
        }
    }
  2. Boolean -> rendered as a CheckBox
    private bool _boolField;
    [WebBrowsable(true), Personalizable(true)]
    public bool BoolField
    {
        get
        {
            return this._boolField;
        }
        set
        {
            this._boolField = value;
        }
    }
  3. Enumerators -> rendered as a ComboBox
    public enum Days
    {
        Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday
     
    }
     
    private Days _enumField;
    [WebBrowsable(true), Personalizable(true)]
    public Days EnumField
    {
        get
        {
            return this._enumField;
        }
        set
        {
            this._enumField = value;
        }
    }

Most of my classes kept the same signature compared to the initial design. For a complete description of all classes their methods,  you can read the second post of the blog series.

 
Class Diagram

To use the UserControlWebPart assembly you must register it in your ASP.NET 2.0 portal application or populate it in SharePoint. A good guide to follow for populating the SharePoint WebParts Gallery is of Mart Muller. See http://blogs.tamtam.nl/mart/CreateASharePoint2007WebpartStepByStep.aspx. It's based on Office SharePoint Server 2007 Beta 1 TR, but still do the job. After this you can use it in any of these apps as a normal WebPart. Your UserControls need to be in the "UserControls" folder of the application. When a UserControl contains a separate assembly file, you must place it in the "bin" folder of the application.

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

As you can see in the image, I now have create my an UserControl EditorPart which customizes / personalizes a UserControl.

Summary
I did it again. This time it costs me several days instead of hours, because retrieving and storing the properties of user control isn't easy. Maybe somebody can confirm this ;-), it would encourage me to finish the blog series. The next part will focus on setting up connectable user controls by using the consumer / provider model of ASP.NET 2.0. Frankly I don't have a clue when I post this blog because I'm working on setting up TFS to run over SSL and off course about WSS 3.0 the MS way. To give everyone including the SharePoint Team the ability to start using this WebPart I included the sources and DLL in the post as well. If somebody has better ideas (e.g. AJAX) or want to help writing this code, send me an email.

 

5 Comments

In mine last post about my own UserControl WebPart, I ended the article with creating a simple editor part. The only thing I didn't show, was how you really could use an editor part to personalize / customize its WebPart. In this article I will show the code and than explain why I coded it this way. So here is the code of the WebPartEditor, which gives a list of all available user controls in de UserControls directory.

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace My.WebParts.Editors
{
    public class WebPartEditor : BaseEditorPart
    {
        public WebPartEditor()
        {
            this.ID = "WebPartEditor";
            this.Title = "Web Part Properties";
        }
 
        private string[] _errorMessages = null;
        private string[] _propertyDisplayNames = null;
        private string[] _propertyDescriptions = null;
 
 
        protected override void CreateChildControls()
        {
            this.Controls.Clear();
 
            UserControlWebPart part = this.WebPartToEdit as UserControlWebPart;
            this._errorMessages = new string[1] { null };
            this._propertyDisplayNames = new string[1] { "User control to display" };
            this._propertyDescriptions = new string[1] { "UserControl" };
 
            if (string.IsNullOrEmpty(part.UserControlPath) || (!Directory.Exists(HttpContext.Current.Server.MapPath(part.UserControlPath))))
            {
                TextBox userControlTextBox = new TextBox();
                userControlTextBox.ID = part.ID + "UserControlWebPartInput";
                userControlTextBox.AutoPostBack = true;
                userControlTextBox.TextChanged += new EventHandler(userControlTextBox_TextChanged);
 
                userControlTextBox.Text = part.UserControl;
                this.Controls.Add(userControlTextBox);
            }
            else
            {
                DropDownList userControlList = new DropDownList();
                userControlList.ID = part.ID + "UserControlWebPartSelect";
                userControlList.AutoPostBack = true;
                userControlList.SelectedIndexChanged += new EventHandler(userControlList_SelectedIndexChanged);
 
                DirectoryInfo directory = new DirectoryInfo(this.Page.Request.MapPath(part.UserControlPath));
                FileInfo[] files = directory.GetFiles("*.ascx");
 
                if (files.Length > 0)
                {
                    userControlList.DataSource = files;
                    userControlList.DataTextField = "Name";
                    userControlList.DataValueField = "Name";
                    userControlList.DataBind();
                    userControlList.Items[0].Selected = true;
 
                    if (!string.IsNullOrEmpty(part.UserControl))
                    {
                        ListItem item = userControlList.Items.FindByValue(part.UserControl.Substring(part.UserControlPath.Length + 1));
                        if (item != null)
                        {
                            userControlList.Items[0].Selected = false;
                            item.Selected = true;
                        }
                        else
                        {
                            this._errorMessages[0] = string.Format("The file {0} does not exist.", part.UserControl);
                        }
                    }
 
                    if (string.IsNullOrEmpty(part.SelectedUserControl))
                    {
                        part.SelectedUserControl = part.UserControlPath + "/" + userControlList.SelectedValue;
                    }
                }
 
                this.Controls.Add(userControlList);
 
            }
        }
 
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.Page != null)
            {
                this.Page.VerifyRenderingInServerForm(this);
            }
            this.EnsureChildControls();
 
            WebControl[] propertyEditors = new WebControl[] { (WebControl)this.Controls[0] };
            RenderPropertyEditors(writer, this._propertyDisplayNames, this._propertyDescriptions, propertyEditors, this._errorMessages);
        }
 
        private string RequestUserControl(string id, string path)
        {
            if (Array.IndexOf<string>(this.Page.Request.Form.AllKeys, this.UniqueID + "$" + id + "UserControlWebPartInput") > -1)
            {
                return this.Page.Request.Form[this.UniqueID + "$" + id + "UserControlWebPartInput"];
            }
            else
            {
                if (this.Page.Request.Form[this.UniqueID + "$" + id + "UserControlWebPartSelect"] == "None")
                {
                    return null;
                }
                else
                {
                    return path + "/" + this.Page.Request.Form[this.UniqueID + "$" + id + "UserControlWebPartSelect"];
                }
            }
        }
 
        void userControlList_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserControlWebPart webPart = this.WebPartToEdit as UserControlWebPart;
 
            webPart.SelectedUserControl = RequestUserControl(webPart.ID, webPart.UserControlPath);
        }
 
        void userControlTextBox_TextChanged(object sender, EventArgs e)
        {
            UserControlWebPart webPart = this.WebPartToEdit as UserControlWebPart;
 
            webPart.SelectedUserControl = RequestUserControl(webPart.ID, webPart.UserControlPath);
        }
 
        public override bool ApplyChanges()
        {
            UserControlWebPart webPart = this.WebPartToEdit as UserControlWebPart;
 
            this._errorMessages = new string[1] { null }; 
            webPart.UserControl = RequestUserControl(webPart.ID, webPart.UserControlPath);
            webPart.SelectedUserControl = webPart.UserControl;
            webPart.LoadUserControl();
 
            return true;
        }
 
        public override void SyncChanges()
        {
        }
 
    } 
}

In the AboutEditor I only added the method RenderContents, which is responsible for rendering the about info as content. Nothing fancy in the code and everything looked rather simple. In this control I will not only add the method RenderContents but also CreateChildControls. The last method is needed, because I'm not only rendering content, I'm creating events as well. As I said before "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.

The first thing which is different comparing to the other editor part. It doesn't inherit from EditorPart but it inherits from BaseEditorPart. This class is an abstract class and has some default implementations for rendering properties. Another difference is the reference between the WebPart and the EditorPart. This can be accomplished by the following line of code.

UserControlWebPart part = this.WebPartToEdit as UserControlWebPart;

CreateChildControls
This method will create the child controls for the editor part. In this method I will detect if the given user controls directory exists and display the user control files in it in a combobox. When it can't find the directory it will give the user a textbox to type the full path to the user control. I used the CreateChildControls method because I want to some eventhandling as well. In this method I add to either of the selected choice an event which will be called after the value of the user control to display changes.

RenderContents
For the rendering I wrote my self a method named RenderPropertyEditors which is stored in the base class BaseEditorPart. This method handles all properties and depending on their type it will be rendered as TextBox (String, DateTime), DropDownList (Enum) or a RadioButton (Boolean). By doing this I can reuse the code for my other editor part which will be displaying the properties of a user control.

RequestUserControl
This method requests the form which UserControl is given or selected by the user. This depends on if the user types the user control in a textbox or selects it from a combobox.

ApplyChanges
Saves the values in an EditorPart control to the corresponding properties in the associated WebPart control. When the values are applied, it loads the selected user control. I set the WebBrowsable property of the UserControl property to false and added a new property SelectedUserControl. The UserControl property will be stored, so the next time the WebPart is rendered it uses te stored UserControl. The SelectedUserControl is only used for the editor parts. By doing so I can eventually reload the selected UserControl for accessing its properties without rendering it to the browser. 

[WebBrowsable(false), Personalizable(true)]
public string UserControl
{
  get
  {
    return this._userControl;
  }
  set
  {
    this._userControl = value;
  }
}
 
[WebBrowsable(false)]
public string SelectedUserControl
{
  get
  {
    string editorUserControl = ViewState["EditorUserControl"] as string;
    if (string.IsNullOrEmpty(editorUserControl))
    {
      return UserControl;
    }
    else
    {
      return editorUserControl;
    }
  }
  set
  {
    ViewState["EditorUserControl"] = value;
  }

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 an editor part which customizes  / personalizes a WebPart.

Summary
Hopefully you're even more impressed as I am, because it's so simple to create such a powerfull EditorPart. Microsoft did a great job and made it possible with the ASP.NET 2.0 Framework to create simple but cool WebParts. In the next part I'm going to create another editor part which will display the properties of the user control.

2 Comments

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.

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.

4 Comments

As discussed in my first post Building my own UserControl WebPart part I I'll extend the class model for the UserControl WebPart project. Besides the methods, all fields and properties will be added to those classes too. In this part I discuss why they are added and where they are used for. At the end a screendump is added of the sample ASP.NET 2.0 portal using a UserControl as a WebPart. This UserControl will be the sample UserControl to use in my solution during all my parts.

 
Class Diagram 

All classes are described below:

UserControlWebPart

  • Properties
    • public string UserControlPath
      Path to the UserControl folder.
    • public string UserControl 
      FileName of the UserControl to load.
    • protected internal Hashtable UserControlProperties
      Stores values of the UserControl properties in a Hashtable. This property is protected internal cause it needs to be called from EditorPart controls.
    • object IWebEditable.WebBrowsableObject
      Gets a reference to the WebPart control to enable it to be edited by custom EditorPart controls.
  • Methods
    • protected override void CreateChildControls
      Composition-based implementation to create any child controls. This method will call LoadUserControl.
    • protected internal void LoadUserControl
      Loads the UserControl dynamically to the page. This methods is protected internal cause it needs to be called from EditorPart controls when properties changes.
    • EditorPartCollection IWebEditable.CreateEditorParts
      A collection of custom EditorPart controls associated with this WebPart control.

AboutEditor

  • Methods
    • protected override void RenderContents
      Renders information about this WebPart control.

BaseEditorPart

  • Methods
    • public void RenderPropertyEditors
      Renders all properties of the UserControl in grid formatted table.

WebPartEditor

  • Methods
    • protected override void CreateChildControls
      Composition-based implementation to create any child controls. Creates a DropDownList when the UserControlPath exists, else TextBox.
    • protected override void RenderContents
      Renders the properties of the EditorPart control using RenderPropertyEditors of the base class.
    • void userControlList_SelectedIndexChanged
      Handles the SelectedIndexChanged event when an user changes the value of the DropDownList. Reloads the selected UserControl to show the properties.
    • void userControlTextBox_TextChanged
      Handles the TextChanged event when an user changes the value of the TextBox. Reloads the given UserControl to show the properties.
    • public override bool ApplyChanges
      Stores the selected UserControl.

UserControlEditor

  • Properties
    • private ArrayList EditorControls
      A list of all EditorControls of all editable properties.
  • Methods
    • protected override void CreateChildControls
      Composition-based implementation to create any child controls.
    • protected override void RenderContents
      Renders the properties of the EditorPart control using RenderPropertyEditors of the base class.
    • private Control CreateEditorControl
      Creates a TextBox, DropDownList or CheckBox depending on the type of the property.
    • private object GetEditorControlValue
      Gets the value given by the user in the WebEditorZone.
    • private PropertyDescriptorCollection GetEditableProperties
      Checks whether a property in the UserControl contains the WebBrowsable attribute.
    • private string GetDescription
      Gets the description of a property using the WebDescription attribute.
    • private string GetDisplayName
      Gets the displayname of a property using the WebDisplayName attribute.
    • public override bool ApplyChanges
      Stores all property values in UserControlProperties Hashtable.

Constructors and methods not used are not mentioned.

In ASP.NET 2.0 you can use a web user control (ascx) as web part because of the ASP.NET 2.0 GenericWebPart class. If the class implements the IWebPart interface it has some extra properties used in webpart Editor. The GenericWebPart control exists to provide a run-time wrapper for server controls that are not WebPart controls, so that such controls can be used in Web Parts pages and applications.

 
UserControl in ASP.NET 2.0 Portal using the GenericWebPart

Unfortunately, this GenericWebPart is not available SharePoint 2007. That's why I build my own UserControl WebPart. This WebPart Control needs to be more dynamic than the GenericWebPart class, cause it needs to load UserControls from in- or external Assemblies using in-line code or code-behind files.


UserControl in ASP.NET 2.0 Portal using the My UserControl WebPart

What I like to accomplish is to dynamically load this usercontrol and still are able to modify all UserControl properties which has WebBrowsable attribute.


UserControl in ASP.NET 2.0 Portal using the GenericWebPart

In the next part I'll will complete the aboutEditor and add code to the UserControlWebPart to dynamically load a control and add the editors to the Editor Zone.

2 Comments

Today I started creating my own UserControl WebPart project. I decided to start from sratch en use the Class Desinger of Visual Studio to model the diagram. I've created five classes where four of them are used for editing the WebPart. Ofcourse I modeled an AboutEditor which inherits from System.Web.UI.WebControls.WebParts.EditorPart to create a top part where the name, version and the author of the control is added.


The other two editors are used to display the properties of the WebPart itself and the UserControl selected / given by the user. Both of the editors inherits form the base class BaseEditorPart which will render the properties and their display names.


Class Diagram


The UserControlWebPart class which inherits from System.Web.UI.WebControls.WebParts.WebPart is used to load the UserControl selected / given by the user. It's very easy to load a control dynamically. The following method will do the trick.

protected override void CreateChildControls()
{
    try
    {
        base.CreateChildControls();
        this.Controls.Clear();
        this._control = (UserControl)Page.LoadControl(this.UserControl);
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}


Besides this, it will also implement the System.Web.UI.WebControls.WebParts.IWebEditable interface. This interface enables you to associate custom EditorPart controls with a server control--such as a WebPart control, a user control, or a custom server control. The EditorPart controls are contained with an EditorZone control, and this zone with its editing controls provides end users with a user interface (UI) for modifying properties, appearance, and behavior on the associated WebPart control.


In the next part I'll will model the public methods in the classes. I also give a screendump of the sample ASP.NET 2.0 portal using a UserControl. This UserControl will be the sample UserControl to use in my solution during all my parts.