Building my own UserControl WebPart part II

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.

4 thoughts on “Building my own UserControl WebPart part II

  1. Maik,

    I'm working on the code right now. I'm able to to select a usercontrol form the usercontrol folder and automatically add it to the page. Things like adding properties and provider/consumer are not. I will post a blog about part III during the weekend.

    Reply

Leave a Reply

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