Building my own UserControl WebPart part I

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.

2 thoughts on “Building my own UserControl WebPart part I

  1. Tony

    This is very cool, and I look forward to seeing more as it matures. I'm interested to know if the User Control implementation will require inline code, or will allow a code-behind file.

    Reply
  2. Anonymous

    I added a user control and the BL and code behind is placed in seperate file i added the user control there is error in page.Can you help me out in solving the problem

    Reply

Leave a Reply

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