Understanding pagination in QuickelSoft CMS
IntroductionThere may be times when you need to display a large number of Content Items on your web page. You should then limit the number of records to display per page and let the user navigate from one page to another. This article will show you how to enable pagination in you ASPX page using the QuickelSoft CMS API. To enable pagination in your ASPX page, you have to: - Split your collection of ContentItems into pages and use the method GetPage of the object ContentItemCollection
- Add a parameter to the query string to indicate which page to display.
- Add navigation links to go to the next page or the previous page.
How to split the list of content items into multiple pagesThe property RecordsByPage of the ContentItemCollection object defines how many content items must be fetched when you call the method GetPage. For instance: ContentItemCollection contentItems = CurrentFolder.GetContentItems(); contentItems.RecordsByPage = 5; contentItems.GetPage(2); In this example, each page will contain 5 content items and the second page (2) is fetched. How to specify which page to displayAdd a parameter called "Page" to your query string to indicate which page to display. public int CurrentPage { get { int currentPage; if (Request["Page"] == "" || Request["Page"] == null) return 1; if (!Int32.TryParse(Request["Page"], out currentPage)) return 1; return currentPage; } } The property CurrentPage returns the value of this parameter if found or 1 if the parameter is not defined or incorrect. Navigation links1. Add two links, one to go the next page and one to go to the previous page. <asp:LinkButton Text="Previous" runat="server" ID="ButtonPrevious" OnClick="ButtonPrevious_Click" /> <asp:LinkButton Text="Next" runat="server" ID="ButtonNext" OnClick="ButtonNext_Click" /> 2. Add the code for the two links. public void ButtonNext_Click(object sender, EventArgs args) { Response.Redirect(CurrentFolder.CurrentLanguageURL + "?Page=" + (CurrentPage + 1)); }
public void ButtonPrevious_Click(object sender, EventArgs args) { Response.Redirect(CurrentFolder.CurrentLanguageURL + "?Page=" + (CurrentPage - 1)); } When you click on the Next button (or the Previous button), the user is redirected to the same folder (CurrentFolder.CurrentLanguageURL) and the page number is added as a parameter in the URL query string. The repeater and data binding1. Add a standard repeater to your page to display the list of content items. <asp:Repeater ID="RepeaterContentItems" runat="server"> <ItemTemplate> <h1> <%# DataBinder.Eval(Container.DataItem, "Title") %> </h1> <%# DataBinder.Eval(Container.DataItem, "Summary") %> <font size="1"> <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:d}") %> </font><a href="<%# DataBinder.Eval(Container.DataItem, "CurrentLanguageURL") %>">Read more ...</a> <br> <br> </ItemTemplate> </asp:Repeater> 2. Bind the data to display the current page. 3. Hide the navigation link “Next” if the last page is displayed or the “Previous” link if the first page is displayed. Full codeprotected void Page_Load(object sender, System.EventArgs e) { int maxPage; ContentItemCollection contentItems = CurrentFolder.GetContentItems(); contentItems.OrderBy = ContentItemOrderBy.Name; contentItems.SortOrder = RecordCollectionSortOrder.Ascending; contentItems.IncludeSubFolders = false; contentItems.RecordsByPage = 5; contentItems.GetPage(CurrentPage); maxPage = contentItems.NumberOfPages; RepeaterContentItems.DataSource = contentItems; RepeaterContentItems.DataBind(); if (CurrentPage == 1) ButtonPrevious.Visible = false; else ButtonPrevious.Visible = true; if (CurrentPage >= maxPage) ButtonNext.Visible = false; else ButtonNext.Visible = true; } public void ButtonNext_Click(object sender, EventArgs args) { Response.Redirect(CurrentFolder.CurrentLanguageURL + "?Page=" + (CurrentPage + 1)); } public void ButtonPrevious_Click(object sender, EventArgs args) { Response.Redirect(CurrentFolder.CurrentLanguageURL + "?Page=" + (CurrentPage - 1)); } public int CurrentPage { get { int currentPage; if (Request["Page"] == "" || Request["Page"] == null) return 1; if (!Int32.TryParse(Request["Page"], out currentPage)) return 1; return currentPage; }
}
|