Datalist Paging

In this post i will explain how to implement paging in DataList C# ASP .NET. Paging in DataGrid is provided by ASP .NET but when in some cases we need to use DataList to show data and also implement paging in that (like a google paging), we have to write code to implement such functionality.

In this post I will explain how to implement paging with Next, Previous, First ad Last page functionality and also a number list of pages for faster access

The Source Code:

HTML in Default.aspx contains two DataList's

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

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

Four LinkButton's to move Next, Previous, First and Last page

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

Now we come to the .cs code. There are three get, set properties

1 - CurrentPage (To keep current page index)

2 - fistIndex

3 - lastIndex

(These two properties are used for paging 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 to get and set various properties from custom paging

PagedDataSource _PageDataSource = new PagedDataSource();

GetDataTable() method simply returns a DataTable.

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;
}
Method BindItemsList() gets DataTable by calling GetDataTable() method
Create DataSource for _PageDataSource, set PageDataSource properties and at the end binds 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 doPaging() method which actually binds paging DataList.
 
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 Call to BindItemList Method at PageLoad()
 
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindItemsList();
        }
    }
 
dlPaging_ItemCommand sets CurrentPage Property and again makes a call to BindItemsList()
    
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 which is currently clicked from paging list
 
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 next, previous, first & last LinkButton's
 
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();
}
 
I am sure if you use this code properly, it will implement paging in your datalist as it did for me.
 
References: 
 
http://www.csharpcorner.com/UploadFile/rizwan328/DataListCustomPaging01112009021450AM/DataListCustomPaging.aspx