我无法保存(或可能加载,或两者兼有)。
自定义属性在属性窗格中显示得很好(嗯,格式不是很好,但是他们在那里)。但我没有做任何事情可以保存属性,或者加载回Web部件或属性窗格。
我不确定自己做错了什么 - 我已经通过了一些教程并且他们都说要做我在这里做的事情或其变体,但没有任何作用。
这是我的代码:
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 :(得分:0)
好像你会过度使用自定义编辑器。如果您只需要这两个Web部件属性,则不需要创建自定义编辑器。
您的文件可能如下所示:
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)
{
}
}
我认为您唯一缺少的是在标记文件中包含<%= this.InvestorPct %>
和<%= this.FounderPct %>
。这将显示这些属性的值。否则它正在保存并加载属性。