Question

I have noticed a bug that occurs on all custom web parts I created.

Let's assume I have following web part (sandbox solution) with two properties:

using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace SampleSandboxSolution.VisualWebPart1
{
    [ToolboxItem(false)]
    public partial class VisualWebPart1 : WebPart
    {
        [Category("Miscellaneous")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Option1")]
        [WebDescription("Option1")]
        public bool Option1 { get; set; }

        [Category("Miscellaneous")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Option2")]
        [WebDescription("Option2")]
        public bool Option2 { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

I've put this web part on my page and I'd like to modify its properties (via "Edit Web Part" from context menu). Option1 and Option2 are unchecked by default. So, I select Option1 and press "OK". Unfortunately, when I enter the properties again without refreshing the page, the checkboxes are unchecked again.

Similar, more annoying scenario:

  1. Check "Option1", leave Option2 unchecked
  2. Press OK and refresh the page
  3. Open web part's properties - Option1 will be checked now, Option2 will be unchecked - as expected
  4. Check Option2 (Option1 and Option2 should be checked now)
  5. Press OK, but do not refresh the page
  6. Open web part's properties - all checkboxes are unchecked again, even Option1!

When I edit web part's properties, it seems that on clicking "OK" button the changes are being saved - but without refreshing the page it does not read those settings until next page load, all the properties are cleared.

Is this SharePoint's bug? Or should I do something to prevent that situation?

Thank you in advance

Was it helpful?

Solution

SharePoint badly displays the properties of type bool, I do not know why. The workaround is to change the type of properties for enum.

    public enum yesNo
    {
        Yes,
        No
    }

    [Category("Miscellaneous")]
    [Personalizable(PersonalizationScope.Shared)]
    [WebBrowsable(true)]
    [WebDisplayName("Option1")]
    [WebDescription("Option1")]
    public yesNo Option1 { get; set; }

If you need a property of type bool, you can hide it in edit mode and adjust it when you change the properties of type enum

    public enum yesNo
    {
        Yes,
        No
    }

    [WebBrowsable(false)]
    public bool Option1 { get; set; }

    private yesNo _option1Enum;

    [Category("Miscellaneous")]
    [Personalizable(PersonalizationScope.Shared)]
    [WebBrowsable(true)]
    [WebDisplayName("Option1")]
    [WebDescription("Option1")]
    public yesNo Option1Enum
    {
        get { return _option1Enum; }
        set
        {
            _option1Enum = value;
            Option1 = _option1Enum == yesNo.Yes;
        }
    }

OTHER TIPS

it should be somthing on the lines of:

Question about webpart properties

// Creates a custom property that is a string.
// This property will be displayed as a text box in the
// property pane.

// Create a custom category in the property sheet.
[Category("Custom Properties")]
// Assign the default value.
[DefaultValue(c_MyStringDefault)]
// Property is available in both Personalization
// and Customization mode.
[WebPartStorage(Storage.Personal)]
// The caption that appears in the property sheet.
[FriendlyNameAttribute("Custom String")]
// The tool tip that appears when pausing the mouse pointer over
// the friendly name in the property pane.
[Description("Type a string value.")]
// Display the property in the property pane.
[Browsable(true)]
[XmlElement(ElementName="MyString")]
// The accessor for this property.
public string MyString
{
    get
    {
        return _myString;
    }
    set
    {
        _myString = value;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top