Question

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();

    }
}
}
Was it helpful?

Solution

Seems like you are going overboard with the custom editor. If you simply need those two web part properties than you don't need to create the custom editor.

Your files can simply look like this:

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"><%= this.InvestorPct %></asp:Label></h3>
                            </td>
                            <td colspan="2">
                                <h3 style="text-align:center"><asp:Label runat="server" ID="lblFounderChargePct"><%= this.FounderPct %> %</asp:Label></h3>
                            </td>
                        </tr>
                    </tbody>
                </table>
        </td>
    </tr>
</table>

ChargeWebPart.ascx.cs:

[ToolboxItemAttribute(false)]
public 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;
        }
    }

    public ChargeWebPart()
    {
    }

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

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

I think the only thing you were missing was including <%= this.InvestorPct %> and <%= this.FounderPct %> in your markup file. Which will display the values of those properties. Otherwise it is saving and loading the properties just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top