Nested Repeater

前端 未结 3 761
孤独总比滥情好
孤独总比滥情好 2020-12-31 17:10

I Have a display that needs to be a little more dynamic than what I\'m use to and can\'t seem to quite find the answer I need.

                        Custom         


        
相关标签:
3条回答
  • 2020-12-31 17:51

    You may use a GridView with AutoGenerateColumns="true". This will create your collumns based on the Datasource you are binding.

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

    Consider this class

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

    And this code

    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();
    

    This will generate an HTML Table with to columns named Field1 and Field2 with the corresponding 3 rows. Somthing like this.

    <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>
    

    If you change the datasource to another source with differnt columns it will automatically generate the corresponding columns for you.

    0 讨论(0)
  • 2020-12-31 17:52

    I know this question is for a datatable, but I found this question while trying to accomplish the same task with objects and I didn't find an answer and thought it would be useful for someone else.

    If you are using an object that has nested objects in it, you set the datasource like this

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

    I came to this conclusion upon all the other answers seeming too complicated

    Here is my full repeater code

    <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>
    
    0 讨论(0)
  • 2020-12-31 18:01

    Nested Repeaters are pretty easy. Just throw one in your ItemTemplate, and in the OnItemDataBound event of your main repeater do the following

    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();
     }
    

    Where the template of the outer repeater had a customer name and a repeater and the inner one has the different savings

    probably incorrect syntax but you get the idea

    <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

    0 讨论(0)
提交回复
热议问题