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 asp.net 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">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label runat="server" ID="lblContactName" Text='<%# Eval("customername") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
<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);   
            }
            else
            {
                Paging.Controls.Clear();
                CreatePaging();
            } 


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;
                Repeater1.DataBind();

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

        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();
                plcPaging.Controls.Add(lnk);
                Label spacer = new Label();
                spacer.Text = "&nbsp;";
                Paging.Controls.Add(spacer);
            }

        }

        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.