在Repeater中渲染自定义复合控件的子控件

时间:2012-11-29 08:56:19

标签: c# asp.net

我有一个从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

}

当我将一些它们添加到占位符控件时,它们呈现正常,但当我尝试将相同的绑定到Repeater时,子复合控件tileControldetailControl不渲染,只有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>

我首先生成一个List<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

0 个答案:

没有答案