我有一部分代码通过在 HTML 中硬编码来嵌套转发器:
<asp:Repeater ID="rpt_1" runat="server">
<ItemTemplate>
<div><%# Eval("ID") %></div>
<asp:Repeater ID="rpt_2" runat="server">
<ItemTemplate>
<div><%# Eval("ID") %></div>
<asp:Repeater ID="rpt_3" runat="server">
<ItemTemplate>
<div><%# Eval("ID") %></div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
每个转发器的 ItemTemplate 具有完全相同的 HTML 和布局内容。有没有办法根据一个的布局以编程方式将它们嵌套 n 个中继器?这是我想做的伪代码:
<asp:Repeater ID="rpt_1" runat="server">
<ItemTemplate>
<div><%# Eval("ID") %></div>
<asp:Repeater ID="rpt_2" runat="server"/>
</ItemTemplate>
</asp:Repeater>
rpt_1.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
rpt_1.DataSource = q;
rpt_1.DataBind();
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (sender is Repeater)
{
string strID = ((Repeater)sender).ID.ToString();
strID = strID.Substring(strID.Length - 1, 1).ToString();
Repeater rpt = (Repeater)e.Item.FindControl("rpt_" + strID);
if (strID != "4")
{//Stop the nested creation
//Does this work?
//How do I accomplish this?
rpt.ItemTemplate = (Repeater)e.Item.FindControl("rpt_" + (Convert.ToInt32(strID)-1).ToString()).ItemTemplate;
rpt_1.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
rpt.DataSource = q;
rpt.DataBind();
}
}
}
我可以在代码中将一个中继器的 ItemTemplate 分配给另一个中继器吗?