开发者

Repeater in Repeater Databinding (no postback)

开发者 https://www.devze.com 2023-01-04 21:14 出处:网络
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.

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>
        </Item开发者_如何学运维template>
    </asp:Repeater>
</div>

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

Thanks for your help!


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

精彩评论

暂无评论...
验证码 换一张
取 消