Sunday, December 8, 2013

Custom paging in ASP.NET with First Last Button

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .grid_paging
        {
            background-color:Green;
            padding: 4px;
            width:238px;
        }
        .grid_paging a
        {
            color: #343434;
            text-decoration: none;
            padding-right: 4px;
            margin-left: 4px;
        }
        .grid_paging a:hover
        {
            text-decoration: underline;
            color:Orange;
            border-color:Orange;
            border-style:solid;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="pager">
        <asp:GridView ID="Gv1" runat="server">
        </asp:GridView>
        <asp:DataList CellPadding="5" RepeatDirection="Horizontal" runat="server" ID="dlPager"
            OnItemCommand="dlPager_ItemCommand" CssClass="grid_paging">
            <ItemTemplate>
                <asp:LinkButton Enabled='<%#Eval("Enabled") %>' runat="server" ID="lnkPageNo" Text='<%#Eval("Text") %>'
                    CommandArgument='<%#Eval("Value") %>' CommandName="PageNo"></asp:LinkButton>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>


 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_PreRender(object sender, EventArgs e)
    {
        // Workaround to prevent clicking twice on the pager to have results displayed properly
        this.Gv1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid(1);
        }
    }
    private void BindGrid(int currentPage)
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee() { ID = 1, FName = "John", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 2, FName = "Mary", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 3, FName = "Amber", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 4, FName = "Kathy", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 5, FName = "Lena", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 6, FName = "John1", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 7, FName = "Mary1", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 8, FName = "Amber1", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 9, FName = "Kathy1", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 10, FName = "Lena1", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 11, FName = "John2", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 12, FName = "Mary2", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 13, FName = "Amber2", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 14, FName = "Kathy2", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 15, FName = "Lena2", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 16, FName = "John2", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 17, FName = "Mary2", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 18, FName = "Amber2", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 19, FName = "Kathy2", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 20, FName = "Lena2", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 21, FName = "John3", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 22, FName = "Mary3", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 23, FName = "Amber3", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 24, FName = "Kathy3", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 25, FName = "Lena3", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 26, FName = "John3", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 27, FName = "Mary3", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 28, FName = "Amber3", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 29, FName = "Kathy3", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 30, FName = "Lena3", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 31, FName = "John4", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 32, FName = "Mary4", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 33, FName = "Amber4", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 34, FName = "Kathy4", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 35, FName = "Lena4", DOB = DateTime.Parse("05/11/1978") });
        empList.Add(new Employee() { ID = 36, FName = "John4", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 37, FName = "Mary4", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 38, FName = "Amber4", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 39, FName = "Kathy4", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 40, FName = "Lena4", DOB = DateTime.Parse("05/11/1978") });
        int TotalCount = empList.Count();
        //var records = from emp in empList
        //              select emp;
        var pgNo = currentPage;
        var pgRec = 10;
        empList = empList.Skip((pgNo - 1) * pgRec).Take(pgRec).ToList();
        Gv1.DataSource = empList;
        Gv1.DataBind();
        generatePager(TotalCount, pgRec, pgNo);
    }
    public void generatePager(int totalRowCount, int pageSize, int currentPage)
    {
        int totalLinkInPage = 3;
        int totalPageCount = (int)Math.Ceiling((decimal)totalRowCount / pageSize);
        int startPageLink = Math.Max(currentPage - (int)Math.Floor((decimal)totalLinkInPage / 2), 1);
        int lastPageLink = Math.Min(startPageLink + totalLinkInPage - 1, totalPageCount);
        if ((startPageLink + totalLinkInPage - 1) > totalPageCount)
        {
            lastPageLink = Math.Min(currentPage + (int)Math.Floor((decimal)totalLinkInPage / 2), totalPageCount);
            startPageLink = Math.Max(lastPageLink - totalLinkInPage + 1, 1);
        }
        List<ListItem> pageLinkContainer = new List<ListItem>();
        
        if (startPageLink != 1)
        {
            int prevcounts = currentPage - 1;
            pageLinkContainer.Add(new ListItem("First", prevcounts.ToString(), currentPage != 1));
        }
        for (int i = startPageLink; i <= lastPageLink; i++)
        {
            pageLinkContainer.Add(new ListItem(i.ToString(), i.ToString(), currentPage != i));
        }
        if (lastPageLink != totalPageCount)
        {
            int Nextcounts = currentPage + 1;
            pageLinkContainer.Add(new ListItem("Last", Nextcounts.ToString(), currentPage != totalPageCount));
        }
       
        dlPager.DataSource = pageLinkContainer;
        dlPager.DataBind();
    }
    protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "PageNo")
        {
            BindGrid(Convert.ToInt32(e.CommandArgument));
        }
    }
    class Employee
    {
        public int ID { get; set; }
        public string FName { get; set; }
        public DateTime DOB { get; set; }
    }
}


 



Img4

0 comments:

Post a Comment