1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Pagination in NewsView

Pagination in NewsView

6 posts, 0 answered
  1. Sheridan
    Sheridan avatar
    13 posts
    Registered:
    02 Jul 2008
    11 Aug 2008
    Link to this post
    I have a NewsView setup and I have it paginating correctly.  However, I have two additional requirements that I haven't been able to nail down.

    1) Display the total number of News Items on the page.  This should be in the form (assuming pagination is 8 per page):

    "1 - 8 of 74" for page 1.
    "9 - 16 of 74" for page 2.
    etc.

    So far I am having problems just displaying the total number on the right.  I started by adding the following code to the repeater's OnPreRender event on ListPageMaster.ascx:

    ContentView contentView = FindParentContentView(repeater);
    ContentManager contentMgr = new ContentManager(contentView.ProviderName);
    numItems = contentMgr.GetContent().Count;

    This got me a grand total, but it did not take into account the current category (We have different pages for different categories).  My next attempt went something like:

    ContentView contentView = FindParentContentView(repeater);
    ContentManager contentMgr = new ContentManager(contentView.ProviderName);
    ICategory category = contentMgr.GetCategory(contentView.CategoryID);
    IMetaSearchInfo[] filter = new IMetaSearchInfo[]
    {
        new MetaSearchInfo(MetaValueTypes.ShortText, "Category", category.CategoryName)
    };
    numItems = contentMgr.GetContent(0, 0, filter).Count;

    Unfortunately contentView.CategoryID does not appear to have a value (it is a GUID filled with all zeroes) so I get a Null Reference when I try to use Category. 

    I have been digging through the API but I haven't found what I need.  Any help would be appreciated.

    Thanks
    Sheridan
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    14 Aug 2008
    Link to this post
    Hello Sheridan,

    Pager control has all the needed properties, so you don't need to retrieve them from database. Adding the following line to Pager control (in ContentViewItemList or any other master template) will do:
    Displaying : <% =(1 + (Me.pager1.SelectedPage - 1) * Me.pager1.PageCount).ToString() + " - " + (Me.pager1.SelectedPage * Me.pager1.PageCount).ToString() + " for page " + Me.pager1.SelectedPage.ToString()%> 

    The reason I am using VB.NET here is that templates (user controls) are by default in VB.NET unless explicitly specified. If you set language to C# in the control declaration you can change this line to C# - if this is something you would like. Anyhow, the whole ListPageMaster.ascx template for NewsView control (~/Sitefinity/ControlTemplates/News/Modes/ListPageMaster.ascx) looks like this after the modification:

    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sf" %> 
     
    <sf:CssFileLink ID="CssFileLink1" FileName="~/Sitefinity/ControlTemplates/News/newsCommonLayout.css" Media="screen" runat="server" /> 
     
    <asp:HyperLink ID="backLink" runat="server" Text="<%$Resources:GoBack %>"></asp:HyperLink> 
    <asp:Repeater ID="repeater" runat="server">  
        <HeaderTemplate> 
            <ul class="sf_newsList">  
        </HeaderTemplate> 
        <ItemTemplate> 
            <li> 
                <h2 class="sf_newsTitle">  
                    <asp:HyperLink ID="fullContent1" runat="server">  
                        <asp:Literal ID="Title" runat="server"></asp:Literal> 
                    </asp:HyperLink> 
                </h2> 
                <class="sf_newsDate">  
                    <asp:Literal ID="Publication_Date" runat="server" Text="{0}" /> 
                </p> 
                <p> 
                    <asp:Literal ID="Summary" runat="server" Text=""></asp:Literal> 
                </p> 
                <class="sf_readMore">  
                    <asp:HyperLink ID="fullContent2" runat="server">  
                        <asp:Literal ID="LiteralFS" runat="server" Text="<%$Resources:FullStory %>"></asp:Literal> 
                    </asp:HyperLink> 
                </p> 
                <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# DataBinder.Eval(Container.DataItem, "Thumbnail").Length %>'>  
                    <class="sf_newsThumbnail"><asp:Image ID="Image1" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "Thumbnail")%>' runat="server" /></p>  
                </asp:PlaceHolder> 
                <class="sf_newsAuthor">  
                        <asp:Literal ID="Author" runat="server"></asp:Literal> 
                </p> 
                <p> 
                    <asp:Literal ID="Source" runat="server"></asp:Literal> 
                </p> 
                <class="sf_postCategory">  
                    <asp:HyperLink ID="Category" runat="server" /> 
                </p> 
                <class="sf_postTags">  
                    <asp:Repeater ID="Tags" runat="server">  
                        <ItemTemplate> 
                            <asp:HyperLink ID="tagLink" runat="server" /> 
                        </ItemTemplate> 
                    </asp:Repeater> 
                </p> 
            </li> 
        </ItemTemplate> 
        <FooterTemplate> 
            </ul> 
        </FooterTemplate> 
    </asp:Repeater> 
    <sf:Pager ID="pager1" runat="server">  
        <LayoutTemplate> 
            <asp:Repeater ID="PageRepeaterLinkButton" runat="server">  
                <HeaderTemplate> 
                    <ol class="sf_pager">  
                    <li><asp:LinkButton ID="PreviousPage" runat="server" Text="<%$Resources:PreviousPage %>"></asp:LinkButton></li>  
                </HeaderTemplate> 
                <ItemTemplate> 
                    <li> 
                        <asp:LinkButton ID="SingleItem" runat="server" /> 
                    </li> 
                </ItemTemplate> 
                <FooterTemplate> 
                    <li><asp:LinkButton ID="NextPage" runat="server" Text="<%$Resources:NextPage %>"></asp:LinkButton></li>  
                    </ol> 
                </FooterTemplate> 
            </asp:Repeater> 
            <asp:Repeater ID="PageRepeaterHyperLink" runat="server">  
                <HeaderTemplate> 
                    <ol class="sf_pager">  
                    <li><asp:HyperLink ID="PreviousPage" runat="server" Text="<%$Resources:PreviousPage %>"></asp:HyperLink></li>  
                </HeaderTemplate> 
                <ItemTemplate> 
                    <li> 
                        <asp:HyperLink ID="SingleItem" runat="server" /> 
                    </li> 
                </ItemTemplate> 
                <FooterTemplate> 
                    <li><asp:HyperLink ID="NextPage" runat="server" Text="<%$Resources:NextPage %>"></asp:HyperLink></li>  
                    </ol> 
                </FooterTemplate> 
            </asp:Repeater> 
            Displaying : <% =(1 + (Me.pager1.SelectedPage - 1) * Me.pager1.PageCount).ToString() + " - " + (Me.pager1.SelectedPage * Me.pager1.PageCount).ToString() + " for page " + Me.pager1.SelectedPage.ToString()%> 
        </LayoutTemplate> 
    </sf:Pager> 

    I hope you'll find this information helpful. Let us know if there is anything else we can do for you.

    Greetings,
    Ivan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Sheridan
    Sheridan avatar
    13 posts
    Registered:
    02 Jul 2008
    14 Aug 2008
    Link to this post
    Ivan,

    Thank you for the reply.  This appears to provide the incorrect information, since Me.pager1.PageCount is actually providing a count of the number of pages rather than the number of items on a page.  So if there are 7 pages with 8 items each you actually get

    Displaying : 1 - 7 for page 1
    Displaying : 8 - 14 for page 2

    instead of

    Displaying : 1 - 8 for page 1
    Displaying : 9 - 16 for page 2

    I've looked through the properties on pager1 and I don't see anything like "NumberOfItemsPerPage"

    Also, the second part of the information I was wanting to display was the total number of items being displayed.  I was able to display total number of news items in the code I posted, but I couldn't figure out how to display total number displayed.  The real problem is that I can't figure out how to determine the category being displayed programmatically.  I am trying to use this code:


    ContentView contentView = FindParentContentView(repeater);
            ContentManager contentMgr = new ContentManager(contentView.ProviderName);
            ICategory category = contentMgr.GetCategory(this.Context.Request[contentView.CategoryKey]);
            ICategory category = contentMgr.GetCategory(contentView.CategoryID);
            IMetaSearchInfo[] filter = new IMetaSearchInfo[]
            {
                new MetaSearchInfo(MetaValueTypes.ShortText, "Category", category.CategoryName)
            };
            numItems = contentMgr.GetContent(0, 0, filter, contentView.ParentIDs).Count;

    But this line:

    ICategory category = contentMgr.GetCategory(this.Context.Request[contentView.CategoryKey]);

    returns null.  Any help in getting this category value would be appreciated, in addition to pointing me to the correct Pager property.

    Thanks
    Sheridan

  4. Sheridan
    Sheridan avatar
    13 posts
    Registered:
    02 Jul 2008
    14 Aug 2008
    Link to this post
    Sorry, disregard the code in the last post.  I was trying different things and didn't realize I had pasted bad code until I re-read it.  The code in the first post shows what I am trying to accomplish (by the way, how do you put your code in code boxes in this editor?)

    ContentView contentView = FindParentContentView(repeater);
    ContentManager contentMgr = new ContentManager(contentView.ProviderName);
    ICategory category = contentMgr.GetCategory(contentView.CategoryID);
    IMetaSearchInfo[] filter = new IMetaSearchInfo[]
    {
        new MetaSearchInfo(MetaValueTypes.ShortText, "Category", category.CategoryName)
    };
    numItems = contentMgr.GetContent(0, 0, filter).Count;

  5. Sheridan
    Sheridan avatar
    13 posts
    Registered:
    02 Jul 2008
    15 Aug 2008
    Link to this post
    Okay, I finally have this worked out and I thought I would share my code in case anyone else wants to see it.

    I made a code behind page and have the language set to C# so I'll show this in pieces.  In ListPageMaster.ascx I have the following code to display my page information.

    <%= (1 + (this.pager1.SelectedPage - 1) * ItemsPerPage).ToString() + " - " + (System.Math.Min(this.pager1.SelectedPage * ItemsPerPage, NumberOfItems)).ToString()  %> of <%# NumberOfItems %> items

    So on page 2 with a setting of 8 items and 52 total items you would see:

    9 - 16 of 52 items

    In
    ListPageMaster.ascx.cs I have two properties:

    public int ItemsPerPage
        {
            get
            {
                ContentView contentView = FindParentContentView(repeater);
                return contentView.ItemsPerPage;
            }
        }
       
        public int NumberOfItems
        {
            get
            {
                return numItems;
            }
        }

    Finally, I added an OnPreRender event handler to the repeater to actually compute the Number of items and set the class variable numItems.  I used PreRender so that any other operations on the repeater (i.e. Databinding) that might affect this could would already be complete.

    protected void repeater_OnPreRender(object sender, EventArgs e)
        {   
            ContentView contentView = FindParentContentView(repeater);
            ContentManager contentMgr = new ContentManager(contentView.ProviderName);
            IMetaSearchInfo[] filter = Array.ConvertAll<string, IMetaSearchInfo>(contentView.SelectedCategories, delegate(string categoryName)
            {
                ICategory category = contentMgr.GetCategory(categoryName);
                return new MetaSearchInfo(MetaValueTypes.ShortText, "Category", category.CategoryName, JoinType.Or);
            });
            numItems = contentMgr.GetContent(0, 0, filter).Count;
        }

    I hope someone finds this helpful
    Sheridan
  6. Sonya
    Sonya avatar
    231 posts
    Registered:
    24 Sep 2012
    18 Aug 2008
    Link to this post
    Hi Sheridan,

    We are glad to hear you found a way to achieve your goal. Thank you for sharing your solution with other community members.

    Sincerely yours,
    Sonya
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
6 posts, 0 answered