动态嵌套中继器 ASP.net
我有一部分代码,通过在 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();
}
}
}
Can I allocate the ItemTemplate of a Repeater to another in code?
I have a portion of code where I nest repeaters by hardcoding them in 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>
Each repeater's ItemTemplate has exactly the same HTML and layout stuff. Is there a way to programatically nest them n number of repeaters based on the layout of one? Here's kind of pseduo code of what I want to do:
<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();
}
}
}
Can I assign the ItemTemplate of one repeater to another in code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 ascx 文件中创建模板,然后按如下方式加载它:
在调用 DataBind() 之前执行此操作;
You can create your template in an ascx file, and then load it like this:
Do this before you call DataBind();