Pagination in Asp.Net Repeater Control using PagedDataSource class

Implementing Pagination while displaying large database in the data-bound controls such as Datagrid, GridView, DetailsView and FormView is termed as the best practice in the modern web development. Here is a snippet of code required to implement pagination while displaying data tables using Repeater control. Pagination in Asp.Net Repeater Control logic works around using PagedDataSource class. Lets look at an example of Pagination in Asp.Net Repeater Control using PagedDataSource class

which is available in System.Web.UI.WebControls. For syntax, properties and method related detailed information on PagedDataSource class, click here .

To start with, Add a repeater control on the .aspx page. Next, add a placeholder control for pagination.

Code on .aspx page looks like below.

 <asp:Repeater ID="Repeater1" runat="server">
                    <asp:Label runat="server" ID="lblContactName" Text='<%# Eval("customername") %>' />
<br> <br>
  <asp:PlaceHolder ID="Paging" runat="server" />

Next, jump to code behind file.

add below snippet to page_load method.

 if (!IsPostBack)
                FetchingData(10, 0);   

Private void FetchingData(int take, int pageSize) 
using (NorthwindDataContext dc = new NorthwindDataContext()) 
{ //fetching data using LINQ query. var query = from p in dc.Customers .OrderBy(o => o.ContactName) .Take(take) .Skip(pageSize) select new { Name = p.ContactName, Count = dc.Customers.Count() };

PagedDataSource page = new PagedDataSource();
                page.AllowCustomPaging = true;
                page.AllowPaging = true;
                page.DataSource = query;
                page.PageSize = 10;
                Repeater1.DataSource = page;

                if (!IsPostBack)
                    RowCount = query.First().Count;

        private void CreatePaging()
            for (int i = 0; i < (RowCount / 10) + 1; i++)
                LinkButton lnk = new LinkButton();               
                lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPage" + (i + 1).ToString();
                lnk.Text = (i + 1).ToString();
                Label spacer = new Label();
                spacer.Text = "&nbsp;";


        void lbl_Click(object sender, EventArgs e)
            LinkButton lnk = sender as LinkButton;
            int currentPage = int.Parse(lnk.Text);
            int take = currentPage * 10;
            int skip = currentPage == 1 ? 0 : take - 10; 
            FetchingData(take, skip);

… Almost done. Now build the solution and run the page in the browser.

Hopefully you will be looking at the pagination implemented for the repeater control. Try Debugging technique to troubleshoot if any error in case.