0

我有一个 sitecore Web 部件,我试图按日期显示前 2 个最新项目。我遇到了麻烦,因为我只能显示最新的项目。

对于 ascx 代码:

    <asp:Repeater ID="newsContainer" runat="server">

        <ItemTemplate>
    <div class="newsItem">
    <h2><a href="/about-us/news.aspx">News</a></h2>
        <a href="<%# SitecoreUtility.NavigateUrl((Item)Container.DataItem)%>" class="newsHeadline">

 <span>  <sc:Date ID="Date1" Item="<%# Container.DataItem %>" Field="Posted-Date" Format="MM.dd.yyyy" runat="server" />    
 <sc:Text ID="Text1" Item="<%# Container.DataItem %>" Field="Title" runat="server" />
    </span>

            </a>

         <!--  <p class="newsSummary">
                <asp:Literal ID="litBody" runat="server" Text="<%# SitecoreUtility.TruncateByWords(((Item)Container.DataItem).Fields[Constants.Fields.BODY].Value, 20) %>"></asp:Literal>
                 ...
                <a href="<%# SitecoreUtility.NavigateUrl((Item)Container.DataItem)%>"     class="more">+ More</a></p> -->
        </div>
    </ItemTemplate>
</asp:Repeater>


<asp:Panel ID="pnlPagination" runat="server"></asp:Panel>
<asp:Panel ID="pnlArchive" runat="server">
  <!--  <div class="newsArchiveLink">
      <a href="?type=archive">View Archived News</a>
    </div> -->
</asp:Panel>
<asp:Panel ID="pnlCurrent" runat="server">
    <div class="newsArchiveLink">
      <a href="?type=current">View Current News</a>
    </div>
</asp:Panel>

对于 ASCX.CS 文件:

public partial class Homepage_NewsListing : BaseSublayout
{
    int CurrentPage = 1;
    int PageSize = Constants.Values.SEARCH_SMALL_LIST;

    protected void Page_Load(object sender, EventArgs e)
    {


        CurrentPage = int.Parse(WebUtil.GetQueryString("page", "1"));

        List<Item> newsListings = new List<Item>();
        Item newsItems = SitecoreUtility.Db.GetItem(Constants.Items.NEWS);

        if (WebUtil.GetQueryString("type", "") == "archive")
        {
            newsListings = newsItems.Children.Where(item =>
                        (SitecoreUtility.FormatDateAtMidnight(item, Constants.Fields.ARCHIVEDATE) <= DateTime.Now)
                        &&
                        (!string.IsNullOrEmpty(item.Fields[Constants.Fields.ARCHIVEDATE].Value))
                    ).OrderBy(item => item.Fields[Constants.Fields.POSTED_DATE].Value).ToList();

            pnlArchive.Visible = false;
            //pnlCurrent.Visible = true;
            pnlCurrent.Visible = false;
        }
        else
        {
            newsListings = newsItems.Children.Where(item =>
                        (SitecoreUtility.FormatDateAtMidnight(item, Constants.Fields.ARCHIVEDATE) > DateTime.Now)
                        ||
                        (string.IsNullOrEmpty(item.Fields[Constants.Fields.ARCHIVEDATE].Value))
                    ).OrderBy(item => item.Fields[Constants.Fields.POSTED_DATE].Value).ToList();

            //pnlArchive.Visible = true;
            pnlArchive.Visible = false;
            pnlCurrent.Visible = false;
        }
        newsContainer.DataSource = DisplayResults(newsListings.OrderByDescending(item => item.Fields[Constants.Fields.POSTED_DATE].Value).ToList());
        newsContainer.DataBind();
    }

    protected List<Item> DisplayResults(List<Item> results)
    {
        pnlPagination.Controls.Add(new Pager(results.Count, PageSize, CurrentPage).BuildPaging());

        if (CurrentPage > 1)
            return results.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();

        return results.Take(PageSize).ToList();
    }
}
4

1 回答 1

1

我会首先尝试使您的代码更简单(用于调试)并使用快速查询来拉取日期范围内的项目。

var date = DateTime.Now.AddMonths(3);
var dateString = date.Year.ToString() + date.Month.ToString().PadLeft(2, '0') +     date.Day.ToString().PadLeft(2, '0');
var newsList = Sitecore.Context.Database.SelectItems("fast:/sitecore/content/home/your-path-to-news/*[@@templatename='Your template name' and @POSTED_DATE > '" + dateString + "']").ToList();
newsList = newsList.OrderByDescending(n => n.Fields["POSTED_DATE"].Value).Take(2).ToList();

newsContainer.DataSource = newsList;
newsContainer.DataBind();
于 2013-02-23T06:05:27.570 回答