Mike's Blog


Business Intelligence keeps me live and communicating!

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.

Comments

Tony said:

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.
# October 15, 2006 1:58 AM

Mike's Blog said:

Tony,

I've added a new article to this blog, explaining how I want to use a UserControl with code-behind files stored in externally added assembly.

# October 16, 2006 12:27 PM

Mike's Blog said:

I recently checked a couple of blogs about using dynamically loaded User Controls. Some of the blogs

# November 14, 2006 11:47 AM

Mike's Blog said:

Since my colleagues Dennis and Alex are writing articles by the minute, it was time for me to do the
# November 15, 2006 9:39 AM

Anonymous said:

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

# June 27, 2007 11:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)