2010-08-11 39 views
0

我正在尋找創建一個項目的可編輯checkboxlist屬性,如下面的代碼所示。編輯界面呈現複選框列表,但不會保留選定的複選框項目。n2cms可編輯的複選框列表屬性

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

有沒有其他人試圖實現上述?如果是這樣,你是如何實現它的?

感謝您的時間和支持

肖恩

回答

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

希望這有助於

肖恩