开发者

nested repeaters with lists of strings and objects

开发者 https://www.devze.com 2023-04-01 21:33 出处:网络
I cant seem to figure out whats wrong with this set of code.I have my front end: <asp:Repeater ID=\"ArchiveYearRepeater\" runat=\"server\" OnItemDataBound=\"ArchiveYearRepeater_ItemDataBound\">

I cant seem to figure out whats wrong with this set of code. I have my front end:

<asp:Repeater ID="ArchiveYearRepeater" runat="server" OnItemDataBound="ArchiveYearRepeater_ItemDataBound">

    <ItemTemplate>
        <div class="ArchiveYear">
            <h3><% DataBinder.Eval(Container.DataItem); %></h3>
            <ul>
                <asp:Repeater ID="ArchivePostRepeater" runat="server">
                    <ItemTemplate>
                        <li><a href="<% DataBinder.Eval(Container.DataItem, "URL"); %>"&g开发者_如何学Ct;<% DataBinder.Eval(Container.DataItem, "Title"); %></a></li>
                    </ItemTemplate>
                </asp:Repeater>
            </ul>
        </div>
    </ItemTemplate>

</asp:Repeater>

Then I have my code behind:

protected void Page_Load(object sender, EventArgs e)
        {


            ArchiveYearRepeater.DataSource = GetYears();
            ArchiveYearRepeater.DataBind();

        }
        protected List<ArchiveItem> GetArchiveItems()
        {
            List<ArchiveItem> ArchiveItems = new List<ArchiveItem>();
            List<BlogPost> posts = BlogPostManager.GetBlogPosts(0, BlogPostManager.BlogSection());

            foreach (BlogPost post in posts)
            {
                ArchiveItem archiveRecord = new ArchiveItem();
                archiveRecord.Title = post.Title;
                archiveRecord.Link = post.URL;
                archiveRecord.Date = post.Date;
                archiveRecord.Year = post.Date.ToString("yyyy");
                ArchiveItems.Add(archiveRecord);
            }

            return ArchiveItems;
        }
        protected List<string> GetYears()
        {
            List<string> Years = new List<string>();
            List<ArchiveItem> ArchiveItems = new List<ArchiveItem>();
            ArchiveItems = GetArchiveItems();
            foreach (ArchiveItem item in ArchiveItems)
            {
                if(!Years.Contains(item.Year)){
                    Years.Add(item.Year);
                }
            }

            return Years;
        }
        private void ArchiveYearRepeater_ItemDataBound(object sender,
    System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if ((item.ItemType == ListItemType.Item) ||
                (item.ItemType == ListItemType.AlternatingItem))
            {
                Repeater ArchivePostRepeater = new Repeater();
                ArchivePostRepeater = (Repeater)item.FindControl("ArchivePostRepeater");

                ArchivePostRepeater.DataSource = GetArchiveItems();
                ArchivePostRepeater.DataBind();
            }
        }

I am trying to make the outer most repeater repeat the years that are found in the main data set of "BlogPost". Then the inner repeater I am trying to write out each post that falls into that year. I am using sharepoint so this thing is throwing errors that I cant quite seem to debug.


Your main problem is that you do not have proper databinding syntax in your aspx code. It should be:

<%# DataBinder.Eval(Container.DataItem, "URL") %>

Also, in your first databinding call, you can do the following because it's a list of strings:

<%# Container.DataItem %>

Beyond that, there are a few other issues with your logic in the ItemDatabound event. You are calling the same GetArchiveItems for every year. Which means that you will get your entire list of ArchiveItems for every year. You should be retrieving the currently bound year and passing that to your GetArchiveItems function so that you can retrieve only those that are related to that year.

Also, you do not need to initialize your ArchivePostRepeater variable to a new Repeater() object, because you are setting it to another one in the code immediately after that. You can just set it to null initially.

0

精彩评论

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

关注公众号