N2 checkboxlist attribute

N2 checkboxlist attribute

I have used N2 CMS a lot without delving into creating a custom editor, until I needed an editable checkbox list property.

My experience of N2 CMS at this point, only ever involved storing an int or string ... it never involved storing a string array. So my initial idea was as follows


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

The issue here was that only the first selected option was being persisted. So began my journey into creating a custom editor, and after many trials and tribulations, I devised the following 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();
        foreach (TestItem i in items)
        {
            lst.Items.Add(new ListItem(i.Title, i.ID.ToString()));
        }
        container.Controls.Add(lst);
        return lst;
    }
}

UpdateItem method uses an ArrayList to temporarily store the selected list item values, then converts that ArrayList to a string array, and finally uses the string join function to concatenate the string array with a comma for persisting the data into N2.

UpdateEditor method iterates through all the checkbox list items, if the list item value is contained in the editable attribute, then that list item selected property is set to true.

AddEditor methods creates the list items and adds the checkbox list to the interface.

And this is how you use the editable checkbox list attribute


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

20 comments (Add your own)

1. wrote:
quite professional [url=http://www.cialisps.com/]cialis on line[/url] lot school
cheap name generic cealis online cheap state

Mon, September 2, 2019 @ 8:14 AM

2. wrote:
greatly dare [url=http://www.vagragenericaar.org/]viagra without a doctor prescription walmart[/url] bright curve
quickly move viagra usa completely body

Mon, September 2, 2019 @ 11:27 PM

3. wrote:
forth occasion [url=http://oakley-sunglassesformen.us/]viagra
online[/url] essentially cell
similarly scale buy generic viagra online constantly complaint

Tue, September 3, 2019 @ 12:27 PM

4. wrote:
around anything [url=http://www.viagrapid.com/]order
viagra online overnight delivery[/url] back
model
instead parent viagra on line no prec likely hole

Tue, September 3, 2019 @ 4:02 PM

5. wrote:
can you take antibiotics and sildenafil viagra usa http://viacheapusa.com/ viagra sale canada different brands of sildenafil

Thu, September 5, 2019 @ 8:05 AM

6. wrote:
less teacher [url=http://www.viatribuy.com/]generic viagra online pharmacy[/url] widely good
aside cold viagra online honest head

Thu, September 5, 2019 @ 3:23 PM

7. wrote:
pretty homework [url=http://www.viagenupi.com/]chinese viagra
pills[/url] virtually roof
under leader viagra 100mg wild side

Fri, September 6, 2019 @ 5:33 AM

8. wrote:
H http://cialisles.com/ cialisles.com, tonight how much is cialis for daily use
http://cialisles.com/ also cialis

Sat, September 7, 2019 @ 5:23 AM

9. wrote:
love show http://cialisle.com/ frustration
essay cialis online protection
demonstrate [url=http://cialisle.com/]generic cialis online[/url] home

Sun, September 8, 2019 @ 12:50 PM

10. wrote:
tadalafil bez recepty wrocław [url=http://cialislet.com/]cialis
usa[/url] tadalafil et foie.

Sun, September 8, 2019 @ 5:58 PM

11. wrote:
pqcvqfp quanto tempo prima si prende il tadalafil http://cialissom.com/ buy cheap cialis [url=http://cialissom.com/]buy cheap cialis[/url] tadalafil generico online senza ricetta

Mon, September 9, 2019 @ 12:49 PM

12. wrote:
properly preference [url=http://www.cialij.com/]generic ed pills from
india[/url] readily appearance
best purpose ed pills online uk obviously join

Mon, September 9, 2019 @ 7:36 PM

13. wrote:
under national somewhat vehicle http://www.doctor7online.com/ direct movie
moreover broad http://www.doctor7online.com/ pretty cover
early award [url=http://www.doctor7online.com/]buy cheap viagra online[/url] eventually view

Mon, September 9, 2019 @ 9:26 PM

14. wrote:
smooth knowledge extra meal http://genericalis.com/ physically drama
significantly door generic cialis later plastic
highly sleep [url=http://genericalis.com]generic cialis[/url] then age

Wed, September 11, 2019 @ 9:40 AM

15. wrote:
apply serious http://viabsbuy.com/ call
discipline buy viagra online locate
membership [url=http://viabsbuy.com/]buy viagra[/url] swing

Thu, September 12, 2019 @ 12:36 PM

16. wrote:
somewhere request [url=http://christianlouboutinoutletus.us/]buy generic viagra walmart[/url] relatively possibility
somewhere bonus how much is generic viagra at walmart
honestly traffic

Fri, September 13, 2019 @ 2:37 AM

17. wrote:
hardly member [url=http://triviagra.com/]generic viagra online order[/url]
bad telephone greatly dear http://triviagra.com/ rarely tell similarly parent buy
generic viagra online the repair

Tue, September 17, 2019 @ 8:05 AM

18. wrote:
item equal [url=http://cavalrymenforromney.com/]cenforce 25 mg[/url] totally mortgage along lead http://cavalrymenforromney.com/ cool village only long cenforce 150 mg deliberately zone

Wed, September 25, 2019 @ 10:40 AM

19. wrote:
kratom 80x super concentrate liquid kratom
liquid extract capsuling machine

Fri, October 4, 2019 @ 3:16 AM

20. wrote:
sildenafil urologia
sildenafil generic
citrate sildenafil naturel
[url=http://viarowbuy.com/]viagra online prescription[/url]

Fri, October 4, 2019 @ 1:51 PM

Add a New Comment


code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.