n2cms 可编辑复选框列表属性

发布于 2024-09-13 11:07:58 字数 546 浏览 2 评论 0原文

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

    [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); }
    }

还有其他人尝试过实施上述内容吗?如果是这样,你是如何实现的?

感谢您的时间和支持

肖恩

I am looking at creating an editable checkboxlist property for an item as indicated in the code below. The edit interface renders the checkboxlist but does not persist the selected checkbox items.

    [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); }
    }

Has anyone else tried to implement the above? If so, how did you achieve it?

Thank you for your time and support

Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-09-20 11:07:58

花了几个小时检查 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, ""); }
}

希望这对

肖恩有帮助

After spending a few hours checking out n2cms custom editors, below is my solution

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;
    }
}

And this is how you use it

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

As an added bonus, here is a radio button list editable attribute

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;
    }
}

And this is how you use it

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

Hope this helps

Sean

百合的盛世恋 2024-09-20 11:07:58

Libardo 在论坛 http://n2cms.codeplex.com/Thread/ 上回答了这个问题View.aspx?ThreadId=223192

这是有关下拉菜单的教程 http://n2cmstutorial.blogspot.com/2010/08/creating-page-with-drop-down-list.html 并且在这些之后实现复选框列表应该不会太难步骤。

Libardo answered this on the forum http://n2cms.codeplex.com/Thread/View.aspx?ThreadId=223192

Here is tutorial on dropdowns http://n2cmstutorial.blogspot.com/2010/08/creating-page-with-drop-down-list.html and it should not be too hard to implement checkbox list following these steps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文