n2cms可编辑复选框列表属性

时间:2010-08-11 11:59:51

标签: c# asp.net n2cms

我正在为项目创建一个可编辑的checkboxlist属性,如下面的代码所示。编辑界面呈现复选框列表,但不会保留选定的复选框项。

    [Editable("Divisions", typeof(CheckBoxList), "SelectedValue", 85, DataBind = true, ContainerName = Tabs.Content)]
    [EditorModifier("DataSource", new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" })]
    public virtual string[] Divisions
    {
        get { return (string[])(GetDetail("Divisions")); }
        set { SetDetail("Divisions", value); }
    }

有没有其他人试图实施上述内容?如果是这样,你是如何实现它的?

感谢您的时间和支持

肖恩

2 个答案:

答案 0 :(得分:1)

花了几个小时检查n2cms自定义编辑器后,下面是我的解决方案

public class EditableCheckBoxListAttribute : AbstractEditableAttribute
{
    public override void UpdateEditor(N2.ContentItem item, Control editor)
    {
        CheckBoxList lst = editor as CheckBoxList;
        if (lst != null)
        {
            foreach(ListItem li in lst.Items)
            {
                if (item[this.Name].ToString().Contains(li.Value))
                {
                    li.Selected = true;
                }
            }
        }
    }

    public override bool UpdateItem(N2.ContentItem item, Control editor)
    {
        CheckBoxList lst = editor as CheckBoxList;
        ArrayList items = new ArrayList();
        foreach (ListItem li in lst.Items)
        {
            if (li.Selected)
            {
               items.Add(li.Value);
            }
        }
        string[] itemID = (string[])items.ToArray(typeof(string));
        item[this.Name] = String.Join(",",itemID);
        return true;
    }

    protected override Control AddEditor(Control container)
    {
        CheckBoxList lst = new CheckBoxList();
        var items = N2.Find.Items
            .Where.Type.Eq(typeof(TestItem))
            .Filters(new NavigationFilter())
            .Select<TestItem>();
        foreach (TestItem i in items)
        {
            lst.Items.Add(new ListItem(i.Title, i.ID.ToString()));
        }
        container.Controls.Add(lst);
        return lst;
    }
}

这就是你如何使用它

[EditableCheckBoxList(Title = "Items")]
public virtual string Items
{
    get { return (string)(GetDetail("Items", "")); }
    set { SetDetail("Items", value, ""); }
}

作为额外的奖励,这是一个单选按钮列表可编辑属性

public class EditableRadioListAttribute : AbstractEditableAttribute
{
    public override void UpdateEditor(N2.ContentItem item, Control editor)
    {
        RadioButtonList rbl = editor as RadioButtonList;
        if (rbl != null)
        {
            rbl.SelectedValue = item[this.Name].ToString();
            if (rbl.Items.FindByValue(item[this.Name].ToString()) != null)
            {
                rbl.Items.FindByValue(item[this.Name].ToString()).Selected = true;
            }
        }
    }

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

    protected override Control AddEditor(Control container)
    {
        RadioButtonList rbl = new RadioButtonList();
        var items = N2.Find.Items
            .Where.Type.Eq(typeof(TestItem))
            .Filters(new NavigationFilter())
            .Select<TestItem>();
        foreach (TestItem i in items)
        {
            rbl.Items.Add(new ListItem(i.Title, i.ID.ToString()));
        }
        container.Controls.Add(rbl);
        return rbl;
    }
}

这就是你如何使用它

[EditableRadioListAttribute(Title = "Item")]
public virtual string Item
{
    get { return (string)(GetDetail("Item", "")); }
    set { SetDetail("Item", value, ""); }
}

希望这有帮助

肖恩

答案 1 :(得分:0)

Libardo在论坛http://n2cms.codeplex.com/Thread/View.aspx?ThreadId=223192

上回答了这个问题

以下是关于下拉列表http://n2cmstutorial.blogspot.com/2010/08/creating-page-with-drop-down-list.html的教程,按照这些步骤实现复选框列表应该不会太难。