1

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

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

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

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

谢谢你的帮助!

4

1 回答 1

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>
于 2010-08-01T13:18:22.697 回答