我有一个从该类派生的自定义控件CompositeControl
,定义为:
[ToolboxData("<{0}:ContractControl runat=server></{0}:ContractControl>")]
public class ContractControl : CompositeControl
{
private int contractID = 0;
private ContractTileControl tileControl = null;
private ContractDetailControl detailControl = null;
private HtmlGenericControl contractMainDiv = null;
public int ContractID
{
get { return this.contractID; }
set { this.contractID = value; }
}
public ContractTileControl TileControl
{
get { return this.tileControl; }
set { this.tileControl = value; }
}
public ContractDetailControl DetailControl
{
get { return this.detailControl; }
set { this.detailControl = value; }
}
public ContractControl()
{
this.contractMainDiv = new HtmlGenericControl("div");
this.contractMainDiv.ID = "contractMainDiv";
this.contractMainDiv.Attributes.Add("class", "contractMain");
}
#region protected override void OnPreRender(EventArgs e)
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
CreateChildControls();
}
#endregion
#region protected override void CreateChildControls()
protected override void CreateChildControls()
{
if (tileControl != null)
{
this.contractMainDiv.Controls.Add(tileControl);
}
if (detailControl != null)
{
this.contractMainDiv.Controls.Add(detailControl);
}
this.Controls.Add(contractMainDiv);
}
#endregion
}
当我将它们中的一些添加到占位符控件时,它们渲染得很好,但是当我尝试将相同的控件绑定到 aRepeater
时,子复合控件tileControl
并且detailControl
不渲染,只有这样contractMainDiv
做。
中继器定义为:
<asp:Repeater ID="myRepeater" runat="server" EnableTheming="true">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr><td><easit:ContractControl ID="contractControl" runat="server" />
</td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
我通过首先生成 aList<ContractControl>
然后调用来绑定它:
List<ContractControl> controls = new List<ContractControl>();
//Generate custom controls for each element in input dictionary
//Create TileControl and DetailControl
//Create ContractControl and add it to collection
ContractControl contractControl = new ContractControl();
contractControl.ContractID = contract.Contract.Id;
contractControl.TileControl = tile;
contractControl.DetailControl = detail;
controls.Add(contractControl);
myRepeater.DataSource = controls;
myRepeater.DataBind();
然而,结果表包含正确数量的项目,但子 'CompositeControl' 根本没有被渲染,只有contractMainDiv
显示。