PlugInProperty as CheckboxList

Vote:
 

Hi!

I am trying to have my plugin have a proprty of a checkbox list to save multiple setting values for my plugin.

I have tried a couple of different ways.

[PlugInProperty(Description = "Tabs to select properties from", AdminControl = typeof(TabSelectorSettingsUi))]

Where TabSelectorSettingsUi is inheriting PropertySettingsControlBase.

I have also tried

public class TabSelectorPropertyControl : PropertySelectMultipleControlBase

I can get both of these to generate a checkbox list but when I try to save I get the same error for both versions:

[NullReferenceException: Object reference not set to an instance of an object.]
   EPiServer.UI.Admin.PlugInAdminSettings.Save() +1239
   EPiServer.UI.Admin.PlugInEdit.Save(Object sender, EventArgs e) +21

What did I miss?

Thanks!

/Kristoffer

#170769
Oct 29, 2016 0:52
Vote:
 

Can you give little a bit more details about the actual property which  attribute applied to!? 

#170809
Oct 31, 2016 10:43
Vote:
 

It is actually not a property or well, maybe it is!? :)

I have a plugin for Episerver and in Admin / Config / Plug-in Manager in the Edit plug-in dialog I want to be able to use this property.
It is actually a tab selector property so to speak. The user should connect one or more tabs to my plugin.

Maybe there is a better may to do this? Select one or more tabs use in a plug-in?

Thanks Aria!

#170812
Oct 31, 2016 11:16
Vote:
 

I think it is better to have property! If it still doesn't work after that if you can share some of the code and I'm more than happy to take a look :)

#170814
Oct 31, 2016 11:23
Vote:
 

So the TabSelectorPropertyControl then is what you mean?

#170816
Oct 31, 2016 11:30
Vote:
 

Something like:

[PlugInProperty(Description = "Tabs to select properties from", AdminControl = typeof(TabSelectorSettingsUi))]

 public CheckBoxList SelectedTabs { get; set; }

I'm not sure about backing type (CheckBoxList) ... it could be string and you can store as comma-separated items! Need to try

#170817
Oct 31, 2016 11:46
Vote:
 
using System;
using System.Web.UI.WebControls;

using EPiServer.PlugIn;

namespace Episerver75.Settings
{
    [GuiPlugIn(Area = PlugInArea.None, DisplayName = "Settings")]
    public class MySettings : IMySettings
    {
        private static MySettings _instance;

        [PlugInProperty(Description = "Tabs to select properties from", AdminControl = typeof(TabSelectorSettingsUi))]
        public string SelectedTabs { get; set; }
       
     
        public MySettings()
        {
            PlugInSettings.SettingsChanged += MySettingsSettingsChanged;
        }

        private static void MySettingsSettingsChanged(object sender, EventArgs e)
        {
            _instance = null;
        }

        public static MySettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AmestoTranslationSettings();
                }

                PlugInSettings.AutoPopulate(_instance);
                return _instance;
            }
        }
        
        
    }
}
using System;

using EPiServer.Core.PropertySettings;
using EPiServer.Data.Dynamic;

namespace Settings.Controls
{
    public class TabSelectorSettings : IPropertySettings
    {
        public TabSelectorSettings()
        {
            SelectedCategories = string.Empty;
        }

        [EPiServerDataMember]
        public string SelectedCategories { get; set; }

        public IPropertySettings GetDefaultValues()
        {
            return new TabSelectorSettings();
        }

        public IPropertySettings Copy()
        {
            var settings = new TabSelectorSettings();
            settings.SelectedCategories = this.SelectedCategories;

            return settings;
        }

        public Guid Id { get; set; }        
    }


}
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

using EPiServer.Core.PropertySettings;

namespace Settings.Controls
{
    public class TabSelectorSettingsUi : PropertySettingsControlBase
    {
        private CheckBoxList tabs;

        protected override void CreateChildControls()
        {

            base.CreateChildControls();

            tabs = new CheckBoxList();
            tabs.Items.Add(new ListItem("Test 0", "0"));
            tabs.Items.Add(new ListItem("Test 1", "1"));
            tabs.Items.Add(new ListItem("Test 2", "2"));

            Controls.Add(tabs);
        }
        
        public override void LoadSettingsUI(IPropertySettings propertySettings)
        {

            EnsureChildControls();

            var tabSelectorSettings = propertySettings as TabSelectorSettings;
            if (tabSelectorSettings == null)
            {
                return;
            }

            var selectedTabs = tabSelectorSettings.SelectedCategories.Split(',');

            foreach (ListItem tab in tabs.Items)
            {
                if(selectedTabs.Contains(tab.Value))
                {
                    tab.Selected = true;
                }
            }
        }

        public override void UpdateSettings(IPropertySettings propertySettings)
        {

            EnsureChildControls();

            var tabSelectorSettings = propertySettings as TabSelectorSettings;
            if (tabSelectorSettings == null)
            {
                return;                
            }

            var selectedTabs = new List<string>();
            foreach (ListItem tab in tabs.Items)
            {
                if (tab.Selected)
                {
                    selectedTabs.Add(tab.Value);
                }
            }

            tabSelectorSettings.SelectedCategories = string.Join(",", selectedTabs);
        }
    }

}

So this is the code example for my test with using settings.

#170818
Oct 31, 2016 12:23
Vote:
 

And here is the property example

using System;
using System.Web.UI.WebControls;
 
using EPiServer.PlugIn;
 
namespace Episerver75.Settings
{
    [GuiPlugIn(Area = PlugInArea.None, DisplayName = "Settings")]
    public class MySettings : IMySettings
    {
        private static MySettings _instance;
 
[PlugInProperty(Description = "Tabs to select properties from", AdminControl = typeof(TabSelectorPropertyControl))]
        public string SelectedTabs { get; set; }
        
      
        public MySettings()
        {
            PlugInSettings.SettingsChanged += MySettingsChanged;
        }
 
        private static void MySettingsSettingsChanged(object sender, EventArgs e)
        {
            _instance = null;
        }
 
        public static MySettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MySettings();
                }
 
                PlugInSettings.AutoPopulate(_instance);
                return _instance;
            }
        }
         
         
    }
}
    using System;

    using EPiServer.Core;
    using EPiServer.PlugIn;

namespace Settings.Controls
{
    [PropertyDefinitionTypePlugIn]
    [Serializable]
    public class TabSelectorProperty : PropertyMultipleValue
    {
        public override IPropertyControl CreatePropertyControl()
        {
            return new TabSelectorPropertyControl();
        }

    }
}
using System.Web.UI.WebControls;

using EPiServer.Core;
using EPiServer.Web.PropertyControls;

namespace Settings.Controls
{
    public class TabSelectorPropertyControl : PropertySelectMultipleControlBase
    {
        protected override bool ShouldCreateDefaultControls()
        {
            return false;
        }

        public PropertyMultipleValue CheckboxListValue
        {
            get { return PropertyData as PropertyMultipleValue; }
        }

        protected override void SetupEditControls()
        {
            SetupDropDownList();
        }

        protected override void CreateChildControls()
        {
            this.SetupDropDownList();
        }

        protected virtual void SetupDropDownList()
        {
            this.AddItems();

            if (CheckboxListValue != null && CheckboxListValue.Value != null)
            {
                foreach (var value in CheckboxListValue.ToString().Split(','))
                {
                    ListItem li = EditControl.Items.FindByValue(value);
                    if (li != null)
                        li.Selected = true;
                }
                
            }
        }

        private void AddItems()
        {
            this.EditControl = new CheckBoxList();
            this.EditControl.EnableViewState = false;
            this.ApplyControlAttributes((WebControl)this.EditControl);
            this.EditControl.RepeatColumns = 3;
            this.EditControl.Style["display"] = "inline";

            EditControl.Items.Add(new ListItem("Test 0", "0"));
            EditControl.Items.Add(new ListItem("Test 1", "1"));

            this.Controls.Add(this.EditControl);
        }        
    }

}

Both of my above example gives me:

[NullReferenceException: Object reference not set to an instance of an object.]
   EPiServer.UI.Admin.PlugInAdminSettings.Save() +1239
   EPiServer.UI.Admin.PlugInEdit.Save(Object sender, EventArgs e) +21
   EPiServer.UI.WebControls.ToolButton.OnClick(EventArgs e) +125
   EPiServer.UI.WebControls.ToolButton.RaisePostBackEvent(String eventArgument) +222
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1734
#170935
Edited, Oct 31, 2016 13:05
Vote:
 

So I solved it.

The key was to use the AdminControlValue on the PluginProperty definition

[PlugInProperty(LanguagePath = "/Translations/Settings/SelectedTabs", AdminControl = typeof(TabSelectorPropertyControl), AdminControlValue = "CheckboxListValue")]

Make a property CheckboxListValue that gets and sets the value of your property then it works just fine.

/Kristoffer

#171060
Oct 31, 2016 22:31
Vote:
 

@Kristoffer: Good one you I just wanted to write down that :D :) but anyway sorry for late response 

#171061
Oct 31, 2016 22:48
Vote:
 

Thanks for your time and help Aria, I really apriciate it!

#171063
Oct 31, 2016 23:24
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.