1

For the solution, I cannot use any postback methods, because this is all working through ajax. The solution need to be implemented in the asp.net code.

I have a List<WebPage> that contains a list of Links (List<Link>) and I need for all the links to bind repetitive information such as page title, id, url. Here is my current repeater.

<div id="result">
    <asp:Repeater runat="server" id="results">
        <Itemtemplate>
            <asp:Repeater runat="server" datasource='<%# Eval("Links") %>'>
                <Itemtemplate>
                    <tr class="gradeX odd">
                        <td><%# Eval("Id") %></td> //property of WebPage (part of results repeater)
                        <td><%# Eval("Title") %></td> //property of WebPage (part of results repeater)
                        <td><%# Eval("Url") %></td> //property of WebPage (part of results repeater)
                        <td><%# Eval("URL") %></td>//Property of Link
                        <td><%# Eval("URLType") %></td> //Property of Link
                        <td><%# Eval("URLState") %></td> //Property of Link
                    </tr>
                </Itemtemplate>
                </asp:Repeater>
        </Itemtemplate>
    </asp:Repeater>
</div>

of course this doesnt work, how can i do this?

Thanks for your help!

Pierluc
  • 395
  • 3
  • 6
  • 13
  • I'm sure you'll find a useful example here http://stackoverflow.com/questions/2923137/repeater-in-repeater – marapet Jun 22 '10 at 19:16
  • Not really, I looked at it already. I really need an asp.net solution. I cannot do such stuff in an ajax based website. – Pierluc Jun 22 '10 at 19:19
  • the page is called through ajax and its returning html code, the actual page that contains the repeater is never visible to the user. After the load method ofthe page is over, the page html code is returned and "bound" with the help of the jQuery pluggins. – Pierluc Jun 22 '10 at 19:37
  • technically its a postback, but its a partial postback. It's more like without page refresh. – Pierluc Jun 23 '10 at 12:24

1 Answers1

1

I'm assuming the problem you're trying to solve here is how to include properties from multiple levels in a nested object-- some properties from the top level but other properties from the lower level. One easy way to do this is to transform the inner collection into a new type which contains a mix of all the properties you need.

Here's a code sample illustrating this technique using IEnumerable.Select and C# Anonymous Classes to create a new class:

<%@ Page Title="Home Page" Language="C#" %>
<div id="result"> 
    <asp:Repeater runat="server" id="results"> 
        <ItemTemplate> 
            <asp:Repeater runat="server" datasource='<%# ((WebPage)Container.DataItem).Links.Select ( link => new {
                                Id = ((WebPage)Container.DataItem).Id, 
                                Title = ((WebPage)Container.DataItem).Title, 
                                Url = ((WebPage)Container.DataItem).Url, 
                                URL = link.URL,
                                URLType = link.URLType,
                                URLState = link.URLState
                                }) %>'> 
                <ItemTemplate> 
                    <tr class="gradeX odd"> 
                        <td><%# Eval("Id") %></td> <!--property of WebPage (part of results repeater) -->
                        <td><%# Eval("Title") %></td> <!--property of WebPage (part of results repeater) -->
                        <td><%# Eval("Url") %></td> <!--property of WebPage (part of results repeater) -->
                        <td><%# Eval("URL") %></td><!--Property of Link -->
                        <td><%# Eval("URLType") %></td> <!--Property of Link--> 
                        <td><%# Eval("URLState") %></td> <!--Property of Link -->
                    </tr> 
                </ItemTemplate> 
                </asp:Repeater> 
        </Itemtemplate> 
    </asp:Repeater> 
</div> 

<script runat="server">
    public class Link
    {
        public string URL { get; set; }
        public int URLType { get; set; }
        public int URLState { get; set; }
    }
    public class WebPage
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Url { get; set; }
        public List<Link> Links { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        WebPage[] pages = new WebPage[] 
        {
            new WebPage { Id = "foo", 
                Title = "foobar", 
                Url = "http://foo.bar", 
                Links = new List<Link> ( new Link[] {
                    new Link {URL = "http://something", URLType = 1, URLState = 2},
                    new Link {URL = "http://someotherthing", URLType = 3, URLState = 4}
                })
            },
            new WebPage { Id = "excellent", 
                Title = "excellent Title", 
                Url = "http://excellent.com", 
                Links = new List<Link> ( new Link[] {
                    new Link {URL = "http://excellent", URLType = 5, URLState = 6},
                    new Link {URL = "http://totallyexcellent", URLType = 7, URLState = 8}
                })
            }

        };
        results.DataSource = pages;
        results.DataBind();
    }
</script>
Justin Grant
  • 44,807
  • 15
  • 124
  • 208