中继器数据绑定中的中继器(无回发)

时间:2010-06-22 19:12:35

标签: c# asp.net data-binding nested-repeater

对于解决方案,我不能使用任何回发方法,因为这都是通过ajax工作的。该解决方案需要在asp.net代码中实现。

我有一个List<WebPage>,其中包含一个链接(List<Link>)列表,我需要所有链接来绑定重复信息,例如页面标题,ID,网址。这是我目前的转发器。

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

当然这不起作用,我该怎么做?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我假设您要解决的问题是如何在嵌套对象中包含多个级别的属性 - 顶层的一些属性,但是较低级别的其他属性。一种简单的方法是将内部集合转换为新类型,其中包含所需的所有属性的混合。

这是一个代码示例,说明了使用IEnumerable.Select和C#匿名类创建新类的技术:

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