Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
How to build an EditorPart for the WebPart framework

Category:  ASP.Net 2.0

With the ASP.Net 2.0’s WebPart Framework, there is something called an Editor Part. An Editor Part is a control that let us change the settings of a WebPart. With ASP.Net 2.0 we have four Editor Parts:

 

AppearanceEditorPart

 

With the AppearanceEditorPart we can specify the WebPart’s Title, Height and Width, the Chrome type and if the WebPart should be hidden or not etc.

 

BehaviorEditorParat

 

With the BehaviorEditrpPart we can specify if the WebPart could be closed, hidden, Minimize and if we should be able to edit the WebPart etc.

 

LayoutEditorPart

 

With the LayoutEditprPart we can specify which Zone the WebPart should be located etc.

 

PropertyGridEditorPart

 

The PropertyGridEditorPart will display a WebPart’s properties marked with the WebBrowsable attribute. This Editpr Part can be used to make it possible to change a WebPart's property that we have added to our own WebPart and are not handled by the rest of the Editor Parts.

 

An Editor Part must be located within an EditorZone. When we edit a WebPart (By selecting the Edit menu option for the WebPart we want to edit) the EditorZone will automatically add an Ok, Apply and Cancel button for saving or cancel the settings we have made for the Webpart we are editing.

 

All of the above Editor Parts inherit the EditorPart class. If we want to build our own Editor Part we need to inherit the EditorPart class. The EditorPart has two methods that must be implemented, ApplyChanges and SuncChanges.

 

The ApplyChanges method purpose is to set the changes we have made for the WebPart we are editing. The ApplyChanges will be executed when we press the Ok button or the Apply button of the EditorZone.

 

The SuncChanges method purpose is to get the values form the WebPart’s properties we are editing, and set them to the controls that will represent the UI of which properties we can change for the WebPart.

 

The following code is an custom EditorPart for changing the settings of our own custom WebPart. This EditroPart will display a ListBox with items and set the ListBox selected value to our own WebPart when we press the Ok or Apply button.

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

 

namespace Test

{

 

    public class myEditorPart : EditorPart

    {

        private DropDownList list1 = new DropDownList();

 

        public myEditorPart()

        {

        }

 

        //Gets the WebPart we are editing and get the value from the selected item the ListBox

        // and set the value to one of the property added to the Webpart.

        protected override bool ApplyChanges()

        {

            MyWebPart myWebPart = WebPartToEdit as MyWebPart;

 

            if (myWebPart != null)

            {

                myWebPart.Item = list1.SelectedIndex;

            }

            return true;

        }

 

 

        //Gets the value from our WebPart's property and use the value to set the slected index of the ListBox.

        public override void SyncChanges()

        {

            MyWebPart myWebPart = WebPartToEdit as MyWebPart;

 

                this.EnsureChildControls();

            if (myWebPart != null)

                list1.SelectedIndex = myWebPart.Item;

        }

 

 

        protected override void CreateChildControls()

        {

            list1.Items.Add(new ListItem("Item1"));

            list1.Items.Add(new ListItem("Item2"));

            list1.Items.Add(new ListItem("Item3"));

            list1.Items.Add(new ListItem("Item4"));

            list1.Items.Add(new ListItem("Item5"));

            this.Controls.Add(list1);

        }

    }

}

 

MyWebPart:

 

using System;

using System.Web.UI.WebControls.WebParts;

 

public class MyWebPart : WebPart

{

    private int _item;

 

    public MyWebPart()

    {

        this._item = 0;

    }

 

 

    [Personalizable]

    public int Item

    {

        get { return this._item; }

        set { this._item = value; }

    }

}

 

To mention something about the code above, I decided to write about those things I have not yet mentioned.

 

To Get the WebPart that we are editing we use the WebPartToEdit property of the EditorClass:

 

MyWebPart myWebPart = WebPartToEdit as MyWebPart

 

The Personalizable attribute added to the Item property of the MyWebPart class, is used to make the value of the property to be automatically saved or set by the WebPart personalization feature.

Posted: Sunday, August 22, 2004 - 13:22 GMT+1    Print     E-mail    Comments (4)
   fredrik.nsquared2.com - 2007