Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET

Efficient Paging with Repeater and SQL Server 2005 / 2008

Rate me:
Please Sign up or sign in to vote.
4.67/5 (6 votes)
16 Dec 2011CPOL2 min read 42K   690   23  
An efficient implementation of paging using a Repeater and the OVER() keyword in SQL Server 2005.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

public partial class paging : System.Web.UI.Page
{
    int pageSize = 15;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getData(1); // initially get data from the first page
            createPaging();
        }
        else
        {
            plcPaging.Controls.Clear();
            createPaging();
        }
    }

    private void getData(int Page)
    {
        DataTable dt = PagingData(Page, pageSize);
        if (dt.Rows.Count > 0)
        {
            hdCount.Value = dt.Rows[0]["TotalRows"].ToString();
            rep1.DataSource = dt;
            rep1.DataBind();
        }
    }

    private DataTable PagingData(int Page, int PageSize)
    {
        // talk to database and return datatable
        Database db = DatabaseFactory.CreateDatabase();
        using (DbCommand cmd = db.GetStoredProcCommand("Paging"))
        {
            db.AddParameter(cmd, "@Page", DbType.Int32, 4, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Default, Page);
            db.AddParameter(cmd, "@PageSize", DbType.Int32, 4, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Default, PageSize);
            using (DataTable dt = (db.ExecuteDataSet(cmd)).Tables[0])
            {
                return dt;
            }
        }
    }

    protected void pager_Click(object sender, EventArgs e)
    {
        LinkButton lnk = sender as LinkButton;
        int currentPage = int.Parse(lnk.Text);
        getData(currentPage);
    }

    private void createPaging()
    {
        int rowCount = Convert.ToInt32(hdCount.Value.ToString());
        if (rowCount <= pageSize) // don't create the pager if there are less rows than specified pageSize.
            return;

        // e.g. 9 % 5 = 4 - means we have an extra page, so add 1 to rowCount otherwise add 0
        rowCount = rowCount / pageSize + (rowCount % pageSize != 0 ? 1 : 0); 

        for (int i = 0; i < rowCount; i++) 
        {
            LinkButton lnk = new LinkButton();
            lnk.Click += new EventHandler(pager_Click);
            lnk.ID = "p" + (i).ToString();
            lnk.Text = (i+1).ToString();
            plcPaging.Controls.Add(lnk);
        }
    }


    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions