0

I can't get my properties to save (or possibly load, or both).

The custom properties show in the property pane just fine (Well, the formatting isn't great, but they're there). But nothing I do can make the properties save, or load back into either the web part or the property pane.

I'm not sure what I'm doing wrong - I've been through a number of tutorials and they all say to do what I'm doing here, or variations on it, but nothing works.

Here's my code:

ChargeWebPart.ascx:

<div>
    <table style="width:100%; border: 1px solid black;">
        <tr>
            <td>
                <h3 style="text-align:center">Charge Calculator</h3>
            </td>
        </tr>
        <tr>
            <td>
                    <table style="width:100%">
                        <tbody>
                            <tr>
                                <td colspan="2">
                                    <h3 style="text-align:center"><asp:Label runat="server" ID="lblInvestorChargePct">x%</asp:Label></h3>
                                </td>
                                <td colspan="2">
                                    <h3 style="text-align:center"><asp:Label runat="server" ID="lblFounderChargePct">y%</asp:Label></h3>
                                </td>
                            </tr>
                        </tbody>
                    </table>
            </td>
        </tr>
    </table>
</div>

ChargeWebPart.cs:

partial class ChargeWebPart : WebPart
{

    private double _investorPct;// = 0.0;
    private double _founderPct;// = 0.0;

    [WebBrowsable(true), Personalizable(PersonalizationScope.Shared)]
    public double FounderPct {
        get
        {
            return _founderPct;
        }
        set
        {
            _founderPct = value;
        }
    }

    [WebBrowsable(true), Personalizable(PersonalizationScope.Shared)]
    public double InvestorPct
    {
        get
        {
            return _investorPct;
        }
        set
        {
            _investorPct = value;
        }
    }
}

ChargeWebPart.ascx.cs:

[ToolboxItemAttribute(false)]  
public partial class ChargeWebPart : WebPart
{
    public ChargeWebPart WebPart { get; set; }

    public ChargeWebPart()
    {
    }

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

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        lblFounderChargePct.Text = FounderPct.ToString() + "%";
        lblInvestorChargePct.Text = InvestorPct.ToString() + "%";

    }


    public override EditorPartCollection CreateEditorParts()
    {
        return new EditorPartCollection(base.CreateEditorParts(),
        new[]
        {
            new CustomEditorPart
            {
                ID = ID + "_editorPart"
            }
        });
    }
}

public class CustomEditorPart : EditorPart
{
    private TextBox _investorPct;
    private TextBox _founderPct;



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

        _investorPct = new TextBox();

        Controls.Add(new LiteralControl("Percentage charge to investor: "));
        Controls.Add(_investorPct);

        _founderPct = new TextBox();

        Controls.Add(new LiteralControl("\r\nPercentage charge to founder: "));
        Controls.Add(_founderPct);


    }

    public override bool ApplyChanges()
    {
        EnsureChildControls();
        double ipct;
        double fpct;

        double.TryParse(_investorPct.Text, out ipct);
        ((ChargeWebPart)WebPartToEdit).InvestorPct = ipct;

        double.TryParse(_founderPct.Text, out fpct);
        ((ChargeWebPart)WebPartToEdit).FounderPct = fpct;

        return true;
    }

    public override void SyncChanges()
    {
        EnsureChildControls();

        var webpart = ((ChargeWebPart)WebPartToEdit);

        _investorPct.Text = webpart.InvestorPct.ToString();
        _founderPct.Text = webpart.FounderPct.ToString();

    }
}
}
0

Actually I don't see anything wrong in your code. I've also tried your code - it works correctly on my SP2013 without any modifications.

Are you trying to edit these properties as site owner or member? Make sure you are not logged in as user with Contribute permission. By default users with Contribute permissions can see web part properties with PersonalizationScope.Shared, but cannot save it - it looks like your case.

| improve this answer | |
  • Thanks for looking. Was logged in as Administrator (i.e. system account) but have just tried with a different account which has full control - same result, properties not saved. – Brongers Croste May 13 '14 at 13:40
  • It seems to have been a problem with my environment, as I deployed the solution elsewhere and it works correctly. – Brongers Croste May 15 '14 at 8:26

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.