Nested Repeater

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.

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

  savings with product b

  (and so on)

I know there will always be a minimum of one in each field. Someone said use a nested repeater or something. I looked around and couldn’t find out how to use a nested repeater. I am on a deadline and can’t really play with things until I find something that works.

What asp control should I use to do this? An example would be nice but I just need help in the right direction.

I am using sql but getting the data through link. The data ends up in lists.

Thank you for your help!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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>

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x