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

发布于 2024-09-06 11:54:57 字数 1344 浏览 6 评论 0原文

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

我有一个 List ,其中包含链接列表(List),我需要所有链接来绑定重复信息,例如页面标题、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>

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

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-09-13 11:54:57

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

下面的代码示例使用 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文