DataList Custom Paging in ASP.Net Using C#

Introduction

This article describes DataList Paging with Next, Previous, First and Last page access functionality. A numbers list is also displayed for accessing pages faster.

datatlist_paging

Background

A few days ago when I was required to implement custom paging in a DataList (like Google paging) I searched the Internet but did not find any perfect code. Then after some effort I wrote my own code. I decided to share it with other users. Surely this will help other developers.

Using the code

Here we will explain the complete example code.

HTML in Default.aspx contains the following two DataLists:

  1. dListItems (Main DataList showing actual contents to implement paging)

  2. dlPaging (used for displaying page numbers as navigation links)

Four LinkButtons to move to the next, previous, first and last page.

A Label to display the current page number out of a total number of pages.

Now we come to the C# code. There are the following three properties:

1. CurrentPage (to maintain the current page index)

2. fistIndex and 3-lastIndex (These two properties are used for paging the DataList)

    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];
            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {
 
            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {
 
            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }

We need a PagedDataSource object to get and set various properties for custom paging.
 

PagedDataSource _PageDataSource = new PagedDataSource();

The GetDataTable() method simply returns a DataTable. In the example code I am not pulling data from the database. I have created a custom DataTable and added 100 rows to the DataTable using a for loop.

    private DataTable GetDataTable()
    {
        DataTable dtItems = new DataTable();
 
        DataColumn dcName = new DataColumn();
        dcName.ColumnName = "title";
        dcName.DataType = System.Type.GetType("System.String");
        dtItems.Columns.Add(dcName);
 
        DataRow row;
        for (int i = 1; i <= 100; i++)
        {
            row = dtItems.NewRow();
            row["title"] = "Sample Row: I am putting here sample text for row " + i;
            dtItems.Rows.Add(row);
 
        }
        return dtItems;
 
    }

The method BindItemsList() gets the DataTable by calling the GetDataTable() method.

Create a DataSource for _PageDataSource, set the PageDataSource properties and at the end bind PageDataSource to dListItems.

    private void BindItemsList()
    {
 
        DataTable dataTable = this.GetDataTable();
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 10;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;
 
        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;
 
        this.dListItems.DataSource = _PageDataSource;
        this.dListItems.DataBind();
        this.doPaging();
    }

BindItemsList() calls the doPaging() method that actually binds the paging DataList as in the following:

    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
 
        fistIndex = CurrentPage - 5;
 
        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }
 
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
 
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
 
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
 
Make a to call to the BindItemList Method at PageLoad() as in the following:
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindItemsList();
        }
    }
dlPaging_ItemCommand sets the CurrentPage Property and again makes a call to BindItemsList() as in the following:
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.BindItemsList();
        }
    }

dlPaging_ItemDataBound will set Enabled equals to false when binding the LinkButton that is currently clicked from the paging list as in the following:

    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;
 
        }
    }

And at the end we have Event Handlers for the next, previous, first & last LinkButtons as in the following:

    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        this.BindItemsList();
 
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.BindItemsList();
 
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
 
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        this.BindItemsList();
 
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
 
        CurrentPage = 0;
        this.BindItemsList();
 
    }

Up Next
    Ebook Download
    View all
    Learn
    View all