Article

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

17 Aug 2016 vikram singh
3 Comments 1686 Views



When there are bulk records to show in the GridView, developers often use paging to divide the complete records into multiple pages to show the records in the GridView.

 

By default gridview have it's property AllowPaging="true" to generate  the paging. To improve the performance of the application we generate the custom paging using many buttons like First, Previous, Numeric, Next And Last.

HTML Markup

In HTML Markup we use pager template for populate the paging.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Paging in Gridview</title>
    <style>
        .btn-primary {
            padding: 5px 10px 5px 10px;
            margin: 3px;
            background: #13bd2b;
            color: #ffffff;
            text-decoration: none;
        }

        .btn-warning {
            padding: 5px 10px 5px 10px;
            margin: 3px;
            background: #ff0000;
            color: #ffffff;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="true" HeaderStyle-BackColor="Red" HeaderStyle-ForeColor="White"
            AllowPaging="true" OnPageIndexChanging="GridView1_IndexChanging" GridLines="Both" Width="600px"
            PageSize="4" OnRowCreated="GridView1_RowCreated" ShowFooter="false" PagerStyle-Height="50px">
            <Columns>
                <asp:TemplateField HeaderText="Student Id">
                    <ItemTemplate>
                        <%#Eval("StudentId").ToString()%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student Name">
                    <ItemTemplate>
                        <%#Eval("StudentName").ToString()%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student Qualification">
                    <ItemTemplate>
                        <%#Eval("StudentQualification").ToString()%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                <br />
                <br />
                <center>
                No Records Found.....</center>
            </EmptyDataTemplate>
            <PagerTemplate>
                <center>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Page" CommandArgument="First"
                    class="btn-primary">First</asp:LinkButton>
                <asp:Label ID="pmore" runat="server" Text="..."></asp:Label>
                <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Page" CommandArgument="Prev"
                    class="btn-primary">Prev</asp:LinkButton>
                <asp:LinkButton ID="p0" runat="server" class="btn-primary">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="p1" runat="server" class="btn-primary">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="p2" runat="server" class="btn-primary">LinkButton</asp:LinkButton>
                <asp:Label ID="CurrentPage" runat="server" Text="Label" class="btn-warning"></asp:Label>
                <asp:LinkButton ID="p4" runat="server" class="btn-primary">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="p5" runat="server" class="btn-primary">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="p6" runat="server" class="btn-primary">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Page" CommandArgument="Next"
                    class="btn-primary">Next</asp:LinkButton>
                <asp:Label ID="nmore" runat="server" Text="..."></asp:Label>
                <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Page" CommandArgument="Last"
                    class="btn-primary">Last</asp:LinkButton>
            </center>
            </PagerTemplate>
        </asp:GridView>
    </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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }
    protected void binddata()
    {
        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");
        
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GeneratePaging();
        }
    }
    protected void GeneratePaging()
    {

        GridViewRow gvrow = GridView1.BottomPagerRow;
        if (gvrow != null)
        {
            Label lblcurrentpage = (Label)gvrow.Cells[0].FindControl("CurrentPage");
            lblcurrentpage.Text = Convert.ToString(GridView1.PageIndex + 1);
            int[] page = new int[7];
            page[0] = GridView1.PageIndex - 2;
            page[1] = GridView1.PageIndex - 1;
            page[2] = GridView1.PageIndex;
            page[3] = GridView1.PageIndex + 1;
            page[4] = GridView1.PageIndex + 2;
            page[5] = GridView1.PageIndex + 3;
            page[6] = GridView1.PageIndex + 4;
            for (int i = 0; i < 7; i++)
            {
                if (i != 3)
                {
                    if (page[i] < 1 || page[i] > GridView1.PageCount)
                    {
                        LinkButton lnkbtn = (LinkButton)gvrow.Cells[0].FindControl("p" + Convert.ToString(i));
                        lnkbtn.Visible = false;
                    }
                    else
                    {
                        LinkButton lnkbtn = (LinkButton)gvrow.Cells[0].FindControl("p" + Convert.ToString(i));
                        lnkbtn.Text = Convert.ToString(page[i]);
                        lnkbtn.CommandName = "PageNo";
                        lnkbtn.CommandArgument = lnkbtn.Text;

                    }
                }
            }
            if (GridView1.PageIndex == 0)
            {
                LinkButton lnkbtn = (LinkButton)gvrow.Cells[0].FindControl("LinkButton1");
                lnkbtn.Visible = false;
                lnkbtn = (LinkButton)gvrow.Cells[0].FindControl("LinkButton2");
                lnkbtn.Visible = false;

            }
            if (GridView1.PageIndex == GridView1.PageCount - 1)
            {
                LinkButton lnkbtn = (LinkButton)gvrow.Cells[0].FindControl("LinkButton3");
                lnkbtn.Visible = false;
                lnkbtn = (LinkButton)gvrow.Cells[0].FindControl("LinkButton4");
                lnkbtn.Visible = false;

            }
            if (GridView1.PageIndex > GridView1.PageCount - 5)
            {
                Label lbmore = (Label)gvrow.Cells[0].FindControl("nmore");
                lbmore.Visible = false;
            }
            if (GridView1.PageIndex < 4)
            {
                Label lbmore = (Label)gvrow.Cells[0].FindControl("pmore");
                lbmore.Visible = false;
            }
        }

    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Pager)
        {
            GridViewRow gvr = e.Row;
            LinkButton lb = (LinkButton)gvr.Cells[0].FindControl("p0");
            lb.Command += new CommandEventHandler(lb_Command);
            lb = (LinkButton)gvr.Cells[0].FindControl("p1");
            lb.Command += new CommandEventHandler(lb_Command);
            lb = (LinkButton)gvr.Cells[0].FindControl("p2");
            lb.Command += new CommandEventHandler(lb_Command);
            lb = (LinkButton)gvr.Cells[0].FindControl("p4");
            lb.Command += new CommandEventHandler(lb_Command);
            lb = (LinkButton)gvr.Cells[0].FindControl("p5");
            lb.Command += new CommandEventHandler(lb_Command);
            lb = (LinkButton)gvr.Cells[0].FindControl("p6");
            lb.Command += new CommandEventHandler(lb_Command);
        }
    }
    protected void GridView1_IndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        binddata();
    }
    void lb_Command(object sender, CommandEventArgs e)
    {
        GridView1.PageIndex = Convert.ToInt32(e.CommandArgument) - 1;
        binddata();
    }

}

 

vikram singh

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

Comments

No coments found to display!

Leave a Comment