动态嵌套中继器 ASP.net

发布于 2024-09-16 22:55:50 字数 2073 浏览 6 评论 0原文

我有一部分代码,通过在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2024-09-23 22:55:50

您可以在 ascx 文件中创建模板,然后按如下方式加载它:

rpt_1.LoadTemplate(template_file.ascx);

在调用 DataBind() 之前执行此操作;

You can create your template in an ascx file, and then load it like this:

rpt_1.LoadTemplate(template_file.ascx);

Do this before you call DataBind();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文