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

他の誰かが上記を実装しようとしましたか? もしそうなら、どのようにそれを達成しましたか?

ご協力ありがとうございました

ショーン

4

2 に答える 2

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, ""); }
}

お役に立てれば

ショーン

于 2010-09-16T15:45:42.233 に答える
0

Libardo はフォーラムでこれに答えましたhttp://n2cms.codeplex.com/Thread/View.aspx?ThreadId=223192

ここにドロップダウンのチュートリアルがありますhttp://n2cmstutorial.blogspot.com/2010/08/creating-page-with-drop-down-list.htmlそして、これらの手順に従ってチェックボックスリストを実装するのはそれほど難しくありません.

于 2010-09-02T09:16:34.093 に答える