Custom Editor not Working

Topics: Developer Forum, Project Management Forum, User Forum
Jan 20, 2009 at 9:30 PM
I tried to create a custom editor but inheriting the AbstractEditableAttribute abstract class. It will be a simple radio button with 2 choices. I was able to get the radio button to appear when I wanted to add the new content type, but it showed out of order. It was on the bottom of the edit content page, below the box that surrounds the regular EditableAttributes such as Navigation Title and Name.

Furthermore, when I tried to save the content page, I got the error "Object must implement IConvertible." After doing a bit of research this seems to have to do with storing data in the database. 

Any help with these two issues would be appreciated.

Here is what the code for the Custom Editor looks like


    public class EditablePageRadioButtonAttribute : AbstractEditableAttribute
    {
        protected override Control AddEditor(Control container)
        {
            RadioButtonList rbl = new RadioButtonList();
            rbl.Items.Add("Option 1");
            rbl.Items.Add("Option 2");
            container.Controls.Add(rbl);
            return rbl;
        }

        public override void UpdateEditor(N2.ContentItem item, Control editor)
        {
            RadioButtonList rbl = editor as RadioButtonList;
            N2.ContentItem storedItem = item[this.Name] as N2.ContentItem;
            if (storedItem != null)
            {
                rbl.SelectedIndex = rbl.Items.IndexOf(new ListItem(storedItem.ID.ToString()));
            }
        }

        public override bool UpdateItem(N2.ContentItem item, Control editor)
        {
            RadioButtonList rbl = editor as RadioButtonList;
            int itemId = rbl.SelectedIndex;
            N2.ContentItem itemToStore = N2.Factory.Persister.Get(itemId);
            item[this.Name] = itemToStore;
            return true;
        }
    }

Thanks
Daniel
Jan 21, 2009 at 9:53 AM
Here is an example of a simple Calendar control wrapped with the N2 Editable Attribute

public class EditablePeterBlumCalendarAttribute : N2.Details.AbstractEditableAttribute
{
    public override bool UpdateItem(N2.ContentItem item, Control editor)
    {
        DateTextBox dateTextBox = (DateTextBox)editor;
        DateTime? dateTime = dateTextBox.DateNullable;
        item[this.Name] = dateTime;
        return true;
    }

    public override void UpdateEditor(N2.ContentItem item, Control editor)
    {
        DateTextBox dateTextBox = (DateTextBox)editor;
        dateTextBox.DateNullable = item[this.Name] as DateTime?;
    }

    protected override Control AddEditor(Control container)
    {
        DateTextBox dateTextBox = new DateTextBox();
        container.Controls.Add(dateTextBox);
        return dateTextBox;
    }
}

---

so try something like

protected override Control AddEditor(Control container)
        {
            RadioButtonList rbl = new RadioButtonList();
            rbl.Items.Add(new ListItem("Option 1", "1"));
            rbl.Items.Add(new ListItem("Option 2", "2"));
            container.Controls.Add(rbl);
            return rbl;
        }


public override void UpdateEditor(N2.ContentItem item, Control editor)
        {
            RadioButtonList rbl = editor as RadioButtonList;
            rbl.SelectedValue = item[this.Name] as string;
        }

        public override bool UpdateItem(N2.ContentItem item, Control editor)
        {
            RadioButtonList rbl = editor as RadioButtonList;
            item[this.Name] = rbl.SelectedValue;
            return true;
        }

or carry on with SelectedIndex if you like and store an integer.

I haven't tested this btw.

Jan 21, 2009 at 1:27 PM
Your suggestion helped get rid of the "Object must implement IConvertible." But the custom control still appears out of order on the Edit Conten Page; it still displays below and outside of the box containing all the other standard controls.
Coordinator
Jan 21, 2009 at 5:55 PM
I can recommend using firebug to analyze the styles of your control and compare it to existing editors that look better. Perhaps if you post a screenshot I can spot some obvious solution.
Jan 21, 2009 at 9:53 PM
Edited Jan 21, 2009 at 10:08 PM
Daniel, what's a base class of your item (that has a property with [EditablePageRadioButtonAttribute] attribute ?

If you happen to derive it from an item of N2.Templates assembly (say, the AbstractContentPage, or AbstractItem), than pay attention to what tab to direct your editor into. If you dig into AbstractPage you'll see it defines 2 tabs: "Content" and "Advanced". If you reference neither of them when applying your attribute to a property, than an editor for that property will appear "out of order", that is: either above or below a tab. To direct your editor into default "Content" tab, the property definition should look like this:

        [EditablePageRadioButton(ContainerName = "content")]
        public string Test {
            get { return this.GetDetail<string>("test", string.Empty); }
            set { this.SetDetail<string>("test", value); }
        }

@Cristian
By the way, why not to generalise the [EditableDropDownAttribute] a bit to support all of the System.Web.UI.WebControls.ListControl descendants (BulletedList, CheckBoxList, ListBox, RadioButtonList -- a handfull reported by Reflector) ? With such a generic [EditableListControlAttribute] at hand, Daniel's task could be solved by a lot simpler code:

    public class EditablePageRadioButtonAttribute: EditableListControlAttribute
    {
        protected sealed override ListControl CreateEditor()
        {
            return new RadioButtonList();
        }
        
        protected sealed override ListItem[] GetListItems()
        {
            //insert your item generation logic here..
            return Array.ConvertAll(
                new[] { "option 1", "option 2" },
                _item => new ListItem(_item));
        }
    }

Update: posted a patch to support the scenario above
Dec 6, 2009 at 10:15 AM

Where I have to add the EditablePageRadioButtonAttribute class?

and how I will a new button in the toolbar

Coordinator
Dec 6, 2009 at 9:24 PM

You add it to your project somewhere close to your content model.

Dec 7, 2009 at 5:24 AM

Thanks for ur help

But How the toolbar will work with the new control I want to add