代码之家  ›  专栏  ›  技术社区  ›  Hazior

嵌套中继器

  •  8
  • Hazior  · 技术社区  · 14 年前

    我有一个显示器,需要有一点更动态比我习惯了,似乎不能完全找到我需要的答案。

                            Customer a     Customer b     Customer c    (and so on)
      savings with product a
    
      savings with product b
    
      (and so on)
    

    我知道每个领域都会有最少一个。有人说用一个嵌套的中继器什么的。我环顾四周,找不到如何使用嵌套的中继器。我在一个最后期限,不能真正发挥的东西,直到我找到了工作。

    我应该使用什么asp控件来执行此操作?举个例子很好,但我只需要在正确的方向上得到帮助。

    谢谢你的帮助!

    3 回复  |  直到 14 年前
        1
  •  17
  •   Community CDub    7 年前

    嵌套中继器非常简单。只需在ItemTemplate中抛出一个,然后在主转发器的OnItemDataBound事件中执行以下操作

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         DataRowView row = (DataRowView)e.Item.DataItem;
    
         Repeater nestedRepeater = e.Item.FindControl("NestedRepeater") as Repeater;
         nestedRepeater.DataSource = getSavingsPerCustomer(row["customerID"]);
         nestedRepeater.DataBind();
     }
    

    其中外部中继器的模板有一个客户名称和一个中继器,而内部中继器有不同的节省

    可能语法不正确,但你明白了

    <asp:repeater ID="outer">
    <HeaderTemplate>
        <div style="float:left">
    </HeaderTemplate>
    <ItemTemplate>
         Customer: <%= Eval(customer)%><br/>
         <asp:repeater ID="NestedRepeater">
              <ItemTemplate>
              Saving: <%= Eval(saving)%><br/>
              </ItemTemplate>
         </asp:repeater>
    </ItemTemplate>
    <FooterTemplate>
        </div>
    </FooterTemplate>
    </asp:repeater>
    

    Similar SO question: Repeater in Repeater

        2
  •  3
  •   FarFigNewton    12 年前

    我知道这个问题是针对一个datatable的,但是我在尝试用对象完成同样的任务时发现了这个问题,我没有找到答案,并且认为它对其他人有用。

    如果您使用的对象中有嵌套对象,那么可以这样设置数据源

    DataSource='<%# Eval("ChildDataSourceProperty") %>'
    

    这是我的全部转发器代码

    <asp:Repeater ID="linkGroups" 
                  runat="server" 
                  DataSource="add your datasource">
        <ItemTemplate>
            <dt><%# Eval("ParentProperty") %></dt>
            <dd>                
                <asp:Repeater ID="links" 
                              runat="server" 
                              DataSource='<%# Eval("ChildDataSourceProperty") %>'>
                    <ItemTemplate>
                        <p><%# Eval("ChildObjectProperty") %></p>
                    </ItemTemplate>
                </asp:Repeater>
            </dd>
        </ItemTemplate>
    </asp:Repeater>
    
        3
  •  1
  •   Carlos Muñoz Boom    14 年前

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
    </asp:GridView>
    

    考虑一下这门课

    public class A
    {
        public string Field1 { get; set; }
        public int Field2 { get; set; }
    }
    

    这个密码呢

    GridView1.DataSource = new List<A>() {
        new A() { Field1 = "a", Field2 = 1 },
        new A() { Field1 = "b", Field2 = 2 },
        new A() { Field1 = "c", Field2 = 3 },
    };
    GridView1.DataBind();
    

    这将生成一个HTML表,其中包含名为Field1和Field2的列以及相应的3行。像这样的事。

    <table>
        <tbody>
            <tr>
                <th scope=col>Field1</th>
                <th scope=col>Field2</th>
            </tr>
            <tr>
                <td>a</td>
                <td>1</td>
            </tr>
            <tr>
                <td>b</td>
                <td>2</td>
            </tr>
            <tr>
                <td>c</td>
                <td>3</td>
            </tr>
        </tbody>
    </table>
    

    如果您将数据源更改为另一个具有不同列的源,它将自动为您生成相应的列。