2

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

  • I don't think you can store properties in a sandboxed solution? Have you seen this: stackoverflow.com/a/7232947 ? – eirikb Feb 27 '14 at 20:57
  • I don't have any problem with storing or persisting properties in sandbox solutions - it works totally fine. There's only problem with displaying properties after any modification (without refreshing the page) as they all disappear (in fact, they really ARE saved and displayed after page refresh). – movax Feb 28 '14 at 10:57
2

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;
        }
    }
0

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;
    }
}
  • I think using old style properties instead of automatic properties is a fix for a property which uses DefualtValue attribute, whereas OP is not using any DefaultValue in the code. – Nadeem Yousuf Feb 28 '14 at 10:41
  • WebPartStorage attribute is not available in sandbox solution. And I always put default values in .webpart file. Modifying and saving properties always works fine, there's only problem in displaying properties after any modification (without refreshing the page) as they all disappear. This causes some problems as users believe their changes were not persisted. I tried replacing automatic properties with old style properties, but it does not make any difference. – movax Feb 28 '14 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.