1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / CheckBoxList WebTypeEditor Collection Property

CheckBoxList WebTypeEditor Collection Property

3 posts, 0 answered
  1. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    04 Jun 2009
    Link to this post

    OK after pulling my hair out I am just going to ask the experts. I am trying to implement a WebTypeEditor that contains a CheckBoxList, which is populated from a DB lookup table and then returns a List<string> value back to my module property.

    I have monkied around with this to the point my code is starting to get ugly. I can see the return Value back to the property is correct and populated when I click the I'm done button, but for whatever reason Sitefinity refuses to persist that property Value.

    Any help would greatly be appreciated.

    My WebTypeEditor

    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Collections.Generic;  
    using Telerik.Web.UI;  
    using GambitCreations.Sitefinity.PurchasingModule;  
    using GambitCreations.Sitefinity.PurchasingModule.Objects;  
     
    namespace GambitCreations.Sitefinity.PurchasingModule.Editors  
    {  
        public class StatusSelector : Telerik.Cms.Web.UI.WebUITypeEditor<List<string>>  
        {  
            private CheckBoxList _checkboxlist = null;  
            private List<string> list;  
     
            public StatusSelector()  
            {  
                // Ensures the child control is available to the Value property.          
                this.EnsureChildControls();  
            }  
     
            /// <summary>  
            ///   
            /// This property is required by WebUITypeEditors.  The underlying Sitefinity Propery Editor      
            /// will use this property to get & set the value of the Property.      
            /// </summary>      
            public override List<string> Value  
            {  
                get 
                {  
                    if (list == null)  
                    {  
                        list = new List<string>();  
                    }  
                    if (_checkboxlist != null)  
                    {  
                        for (int i = 0; i < this._checkboxlist.Items.Count; i++)  
                        {  
                            if (_checkboxlist.Items[i].Selected)  
                            {  
                                list.Add(_checkboxlist.Items[i].Text);  
                            }  
                        }  
                    }  
                    return list;  
                }  
                set 
                {  
                    list = value;  
     
                    if (list == null)  
                    {  
                        // If our RadioButtonList selected item is out of sync with the Value, then                  
                        // use the current Value to set the selected item.                  
                        SelectDefaultItem();  
                    }  
                }  
            }  
            /// <summary>   
            /// Populate our WebEditor with child controls.      
            /// </summary>      
            protected override void CreateChildControls()  
            {  
                PurchasingManager manager = new PurchasingManager();  
                Controls.Clear();  
                Controls.Add(new LiteralControl("Select an item from the list below:"));  
                _checkboxlist = new CheckBoxList();  
                // Attach an EventHandler that will be fired when a new RadioButtonList item is selected.   
                //_checkboxlist.SelectedIndexChanged += new System.EventHandler(checkboxlist_SelectedIndexChanged);  
                _checkboxlist.EnableViewState = true;  
                //_checkboxlist.AutoPostBack = true;  
                List<PurchasingLookup> statuses = manager.GetLookupListingByType("STATUS");  
                foreach (PurchasingLookup lookup in statuses)  
                {  
                    ListItem item = new ListItem();  
                    item.Text = lookup.LookupDescription;  
                    item.Value = lookup.LookupValue;  
                    _checkboxlist.Items.Add(item);  
                }  
     
                Controls.Add(_checkboxlist);  
            }  
            /// <summary>  
            ///Executed when a new item is selected from the RadioButtonList      
            /// </summary>      
            void checkboxlist_SelectedIndexChanged(object sender, System.EventArgs e)  
            {  
                list.Clear();  
                if (_checkboxlist != null)  
                {  
                    for (int i = 0; i < this._checkboxlist.Items.Count; i++)  
                    {  
                        if (_checkboxlist.Items[i].Selected)  
                        {  
                            list.Add(_checkboxlist.Items[i].Text);  
                        }  
                    }  
                }  
                Value = list;  
            }  
            /// <summary>  
            /// When the WebEditor is loaded, this presets the selected item to the previous value.      
            /// </summary>      
            private void SelectDefaultItem()  
            {  
                if (Value == null)  
                {  
                    Value = new List<string>();  
                }  
                foreach (string value in (List<string>)Value)  
                {  
                    ListItem item = _checkboxlist.Items.FindByText(value);  
                    if (item != null)  
                    {  
                        item.Selected = true;  
                    }  
                }  
            }  
        }  

    My Module Property
    /// <summary>  
            /// Gets or sets the Default Statuses for the Grid  
            /// </summary>  
            /// <value></value>  
            /// <returns></returns>  
            [Category("Settings")]  
            [Telerik.Cms.Web.UI.WebEditor("GambitCreations.Sitefinity.PurchasingModule.Editors.StatusSelector, GambitCreations.Sitefinity.PurchasingModule")]  
            public List<string> DefaultStatus  
            {  
                get 
                {  
                    return this.defaultStatus;  
                }  
                set 
                {  
                    this.defaultStatus = new List<string>(value);  
                }  
            } 
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    04 Jun 2009
    Link to this post
    Hello Garry Clark,

    ok you are half way done with this. Now, what you need to do is to implement TypeConverter. Let me give some background on this:

    - The TypeEditor you have implemented allows you to create a more sophisticated user interface for editing a property.
    - Sitefinity persists all the properties as strings, and since your property is of type List<string> it cannot persist it out of the box. However, to overcome this you can implement a TypeConverter which will convert the List<string> object to a string and vice versa (from string to List<string>) - basically you are doing serialization, deserialization here.

    Actually we have a sample for this in our Developer Manual in Using Non-string properties with Controls topic.

    Since we are on the topic of controls and more advanced implementations, I'll be free to suggest you a webinar that deals with this matter: http://tv.telerik.com/sitefinity/webinar/sitefinity_controls_advanced_topics

    If I recall correctly, the webinar explains TypeConverters among other things.

    I hope you will find this information helpful. Let us know if there is anything else we can do for you.


    Best wishes,
    Ivan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    04 Jun 2009
    Link to this post
    Ivan,
    You know that sad thing is that I have actually watched your Webinar on this subject and after you presented me with the solution I could only hang my head in shame. 

    For whatever reason this just did not pop into my head. I have changed my editor to just deal with a delaminated string and string[] and all is happy in the world.

    Thanks again!
Register for webinar
3 posts, 0 answered