Article

ASP.Net DataList Custom Paging Like First, Previous, Numeric, Next And Last

18 Aug 2016 vikram singh
1 Comments 1022 Views



In this article i explain how to populate paging (Pagination) in DataList control. By default DataList control does not have the paging option hence we create a custom paging. Here we give the example the custom paging like First, Previous, Numeric,

HTML Markup

In HTML Markup we used the repeater for populate the paging. 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Paging in DataList</title>
    <style>
        .btn-primary {
            padding: 5px 10px 5px 10px;
            margin: 3px;
            background: #13bd2b;
            color: #ffffff;
            text-decoration: none;
        }
        .aspNetDisabled {
            padding: 5px 10px 5px 10px;
            margin: 3px;
            background: #ff0000;
            color: #ffffff;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <asp:DataList runat="server" ID="dListItems" CellPadding="5" RepeatColumns="2" RepeatDirection="Horizontal" Width="500px">
                        <ItemTemplate>
                            <div style="border: 1px solid black; background-color: #e4d8d8">
                                <table width="100%">
                                    <tr style="background-color: #ff0000; padding: 10px;">
                                        <td>Student Id : <%#Eval("StudentId") %></td>
                                    </tr>
                                    <tr>
                                        <td>Student Name : <%#Eval("StudentName") %></td>
                                    </tr>
                                    <tr>
                                        <td>Student Qualification : <%#Eval("StudentQualification") %></td>
                                    </tr>
                                </table>
                            </div>
                        </ItemTemplate>
                        <ItemStyle BackColor="White" />
                    </asp:DataList>
                </td>
            </tr>
            <tr>
                <td>&nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                    <table cellpadding="0" border="0">
                        <tr>
                            <td align="right">
                                <asp:LinkButton ID="lbtnFirst" class="btn-primary" runat="server" CausesValidation="false" OnClick="lbtnFirst_Click">First</asp:LinkButton>
                                &nbsp;</td>
                            <td align="right">
                                <asp:LinkButton ID="lbtnPrevious" class="btn-primary" runat="server" CausesValidation="false" OnClick="lbtnPrevious_Click">Previous</asp:LinkButton>&nbsp;&nbsp;</td>
                            <td align="center" valign="middle">
                                <asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
                                    OnItemDataBound="dlPaging_ItemDataBound">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkbtnPaging" class="btn-primary" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                            CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>&nbsp;
                                    </ItemTemplate>
                                </asp:DataList>
                            </td>
                            <td align="left">&nbsp;&nbsp;<asp:LinkButton ID="lbtnNext" class="btn-primary" runat="server" CausesValidation="false"
                                OnClick="lbtnNext_Click">Next</asp:LinkButton></td>
                            <td align="left">&nbsp;
                                <asp:LinkButton ID="lbtnLast" class="btn-primary" runat="server" CausesValidation="false" OnClick="lbtnLast_Click">Last</asp:LinkButton></td>
                        </tr>
                        <tr>
                            <td colspan="5" align="center" style="height: 30px" valign="middle">
                                <asp:Label ID="lblPageInfo" runat="server"></asp:Label></td>
                        </tr>
                    </table>
                    </center>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    #region Private Properties
    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; }
    }
    #endregion

    #region PagedDataSource
    PagedDataSource _PageDataSource = new PagedDataSource();
    #endregion

    #region Private Methods
    /// <summary>
    /// Build DataTable to bind Main Items List
    /// </summary>
    /// <returns>DataTable</returns>
    private DataTable GetDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("StudentId", typeof(Int32));
        dt.Columns.Add("StudentName", typeof(string));
        dt.Columns.Add("StudentQualification", typeof(string));
        dt.Rows.Add(1, "Vikram Singh", "B.Tech");
        dt.Rows.Add(2, "Uma Shankar Rai", "MCA");
        dt.Rows.Add(3, "Naresh Chandra", "MCA");
        dt.Rows.Add(4, "Naga Bhusan Jena", "B.Tech");
        dt.Rows.Add(6, "Anand Chauhan", "B.Tech");
        dt.Rows.Add(7, "Zaki Ahmad", "B.Tech");
        dt.Rows.Add(8, "Isha Phatik", "B.Tech");
        dt.Rows.Add(9, "Bikas Kumar", "B.Tech");
        dt.Rows.Add(10, "Dev Yadav", "B.Tech");
        dt.Rows.Add(11, "Pankaj Pratap", "B.Sc");
        dt.Rows.Add(12, "Neeta Devi", "B.Ed");
        dt.Rows.Add(13, "Raj Kuma", "M.A.");
        dt.Rows.Add(14, "Rohan Singh", "B.Tech");
        dt.Rows.Add(15, "Monu Pal", "M.Tech");
        dt.Rows.Add(16, "Hardeep Kaur", "M.Tech");
        return dt;

    }

    /// <summary>
    /// Binding Main Items List
    /// </summary>
    private void BindItemsList()
    {

        DataTable dataTable = this.GetDataTable();
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 6;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.lbtnPrevious.Visible = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Visible = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Visible = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Visible = !_PageDataSource.IsLastPage;

        this.dListItems.DataSource = _PageDataSource;
        this.dListItems.DataBind();
        this.doPaging();
    }

    /// <summary>
    /// Binding Paging List
    /// </summary>
    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();
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindItemsList();
        }
    }
    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 dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.BindItemsList();
        }
    }
    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;
        }
    }
    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();
    }
}

 

vikram singh

I am asp.net developer with 3+ years exp. in asp.net, jquery, javascript,SQL area knowledege.

Comments

  1. Nice .... Good one .. Keep it up...

Leave a Comment