N2CMS

Custom editors

The attributes defining editors on an item is responsible of adding the editor control to the web page and to bind data back and from the content item to be edited.

Lets say we want a drop down with the option of selecting one of the first level items. How could we accomplish this? Behold the IEditable interface. We can create our own attribute implementing this interface and add it to a property. So how does this work?

We might as well use the AbstractEditableAttribute and a few things for free. 

Update: The methods have changed somewhat but the usage is roughly the same. The methods to override are these:

       public override void UpdateEditor(ContentItem item, Control editor) {}

        // Returns:
        //     True if the item was changed (and needs to be saved).
        public override bool UpdateItem(ContentItem item, Control editor) {}
        protected override Control AddEditor(Control container) {}

Lets start by implementing the AddTo method. This is invoked when it's time to add a control to a the form when we're editing an item.

Basically we're binding the first level pages to a drop down list and adding it to the container.

Next is the UpdateEditor method. This is where the editor is bound to the content item we're editing.

Finally we need to update the item we were editing once we have selected something in the drop down list. This is done in the UpdateItem method.

Now we have a nice little attribute we can add to our properties whenever we need this particular functionality.

The editable attribute is also responsible of adding the label to the form. Looking up a way to do this in the N2 source code is left as an excersise for the reader.