How to set Multiple selection in Custom ListBox Property

Vote:
 

Hi, With the following Code I'm able to create a Custom ListBox property.

I would like to use Multiple selection of the list Box. In edit mode I'm able to select multiple values, but after publishing the page I'm seeing only one value as selected value.

Please check the code and advice me what I'm missing here.

    [Serializable]
    [PageDefinitionTypePlugIn]
    public class SignPostListBox:EPiServer.Core.PropertyString
    {
        public override EPiServer.Core.IPropertyControl CreatePropertyControl()
         {
             return new SignPostListBoxControl();
         }
    }

public class SignPostListBoxControl : EPiServer.Web.PropertyControls.PropertySelectMultipleControlBase
    {
        public SignPostListBox SignPostListBox
        {
            get
            {
                return PropertyData as SignPostListBox;
            }
        }

        public override void CreateEditControls()
        {

            lbSignPost = new ListBox();
            lbSignPost.SelectionMode = ListSelectionMode.Multiple;

            //lstSignPost- A list<string> with some values in it.
            lbSignPost.Items.Add(new ListItem(Select, ""));

            if (lstSignPost != null)
            {
                for (int i = 0; i < lstSignPost.Count; i++)
                {
                    lbSignPost.Items.Add(new ListItem("[" + lstSignPost[i].SignpostId.ToString() + "] " + lstSignPost[i].Name, lstSignPost[i].SignpostId.ToString()));
                }
            }

            this.ApplyControlAttributes(lbSignPost);
            Controls.Add(lbSignPost);
            this.SetupEditControls();
        }

        protected override void SetupEditControls()
        {
            try
            {
                lbSignPost.SelectedValue = this.ToString();
            }
            catch
            {
            }
        }

        public override void ApplyEditChanges()
        {
            try
            {
                base.SetValue((lbSignPost.SelectedValue));
            }
            catch
            {
            }
        }

        public static string Translate(string langKey)
        {
            if (string.IsNullOrEmpty(
                     EPiServer.Personalization.EPiServerProfile.Current.Language))
                return LanguageManager.Instance.Translate(langKey);

            //translate based on user language
            return LanguageManager.Instance.Translate(langKey,
                     EPiServer.Personalization.EPiServerProfile.Current.Language);
        }

    }

Please suggest me your Ideas.

Thanks.

#75231
Sep 21, 2013 8:45
Vote:
 

Try using lbSignPost.Items.OfType<ListItem>().Where(item => item.Selected);

Hope this helps.

Frederik

#75232
Sep 21, 2013 11:43
Vote:
 

Hi,

Thanks for the reply.

I tried like this:

protected override void SetupEditControls()
 {
  try
   {
     lbSignPost.SelectedValue = this.ToString();
   }
  catch
    {
    }
 }

public override void ApplyEditChanges()
{
  try
  {
   base.SetValue((lbSignPost.Items.OfType<ListItem>().Where(item => item.Selected)));
  }
  catch
  {
  }
 }

This time I'm not seeing any item as selected once published, Am I missing something in "SetupEditControls()" method.

Thanks.

#75235
Sep 23, 2013 9:25
Vote:
 

When setting the value (ApplyEditChanges()) you need to use a separator, comma or similar. Then when getting the value traverse through the Items and check if the item value matches the one you have stored, and based on that set Selected property to true for that item.

Frederik

#75236
Sep 23, 2013 10:17
Vote:
 

Hi,

With the help of following code I'm able to do this.

        protected override void SetupEditControls()
        {
            try
            {
                string strTempHideSignPostId = string.Empty;
                strTempHideSignPostId=this.ToString();
                List<string> lstSignPosts=new List<string>();
                
                if (strTempHideSignPostId.Contains(","))
                {
                    lstSignPosts = strTempHideSignPostId.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                }
                else
                    lstSignPosts.Add(strTempHideSignPostId);

                lstSignPosts.ForEach(li =>
                {
                    if (lbSignPost.Items.OfType<ListItem>().Any(li2 => li == li2.Value))
                        lbSignPost.Items.OfType<ListItem>().Single(li3 => li3.Value == li).Selected = true;
                });
            }
            catch
            {
            }
        }

        public override void ApplyEditChanges()
        {
            try
            {
                string strSelectedValue = string.Empty;
                List<ListItem> LstTest = new List<ListItem>();
                strSelectedValue = lbSignPost.SelectedValue;
                LstTest = lbSignPost.Items.OfType<ListItem>().Where(a => a.Selected).ToList();

                int itemCount = 0;
                foreach (var item in LstTest)
                {
                    if (itemCount == 0)
                    {
                        strSelectedValue = item.Value.ToString();
                    }
                    else
                        strSelectedValue = strSelectedValue + "," + item.Value.ToString();
                    itemCount++;
                }
                base.SetValue(strSelectedValue);
            }
            catch
            {
            }
        }

Thanks.

#75251
Sep 23, 2013 17:34
Vote:
 

Extract "," to constant, makes it easy when it will come to refactoring :)

Also you can use string.Join() to get things back concatenated.

#75264
Sep 23, 2013 22:30
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.