<%@ 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>
a{padding:5px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="Dgpager" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None" Width="447px">
<AlternatingItemStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
</asp:DataGrid>
<div id="pagination">
<asp:Literal ID="litPaging" runat="server" EnableViewState="true"></asp:Literal>
</div>
</div>
</form>
</body>
</html>
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int totalRecords = 234;
int pageSize = 10;
int totalPages = totalRecords / pageSize + (totalRecords % pageSize > 0 ? 1 : 0);
int NoofNumaricLink = 7;
int currentPage = 1;
if (Request.QueryString["page"] == null)
currentPage = 1;
if (Request.QueryString["page"] != null)
{
if (!int.TryParse(Request.QueryString["page"].ToString(), out currentPage))
currentPage = 1;
if (currentPage > totalPages)
currentPage = totalPages;
}
List<Customer> customers =(new Customer()).GetCustomerList();
var ObjCustomerslist = customers.Skip((currentPage - 1)*pageSize).Take(pageSize);
Dgpager.DataSource = ObjCustomerslist;
Dgpager.DataBind();
litPaging.Text = CreatePageLinks(totalRecords, pageSize, totalPages, NoofNumaricLink,currentPage);
}
string CreatePageLinks(int totalRecords, int pageSize, int totalPages, int TotalNoLink, int currentPage)
{
if (totalRecords <= pageSize)
return "";
StringBuilder strPager = new StringBuilder();
string pageUrl = Context.Request.Url.AbsolutePath;
if (currentPage > 1)
{
strPager.Append(string.Format("<a href='{1}?page={0}' title='First page'>Fist</a>", 1, pageUrl));
strPager.Append(string.Format("<a href='{1}?page={0}' title='Previous page'>Previous</a>", (currentPage - 2) + 1,
pageUrl));
}
if(!(currentPage > 1))
{
strPager.Append("<a herf='javascript:void(0);' title='First Page'>First</a><a herf='javascript:void(0);' title='Previous
page'>Previous</a>");
}
int min, max;
if (TotalNoLink >= totalPages)
{
min = 1;
max = totalPages;
}
else
{
if (currentPage - TotalNoLink / 2 > 0)
max = (currentPage + TotalNoLink / 2 - (TotalNoLink - 1) % 2);
else
max = TotalNoLink;
if (max > totalPages) max = totalPages;
min = max - TotalNoLink + 1 > 0 ? max - TotalNoLink + 1 : 1;
}
for (int n = min; n <= max; n++)
{
if (n > totalPages)
break;
if (n == currentPage)
strPager.Append(String.Format(" {0}", n.ToString()));
else
strPager.Append(String.Format("<a href='{2}?page={1}' title='{0}'>{0}</a> ", n, (n - 1) + 1,pageUrl));
}
if (currentPage < totalPages)
{
strPager.Append(String.Format("<a href='{1}?page={0}' title='Next Page'>Next</a> ", currentPage + 1, pageUrl));
strPager.Append(String.Format("<a href='{1}?page={0}' title='Last Page'>Last</a> ", totalPages, pageUrl));
}
if (!(currentPage < totalPages))
{
strPager.Append("<a herf='javascript:void(0);' title='Next Page'>Next</a><a herf='javascript:void(0);' title='Last
page'>Last</a>");
}
return strPager.ToString();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Customer
{
string uid = string.Empty;
public string Uid
{
get { return uid; }
set { uid = value; }
}
string name = string.Empty;
public string Name
{
get { return name; }
set { name = value; }
}
int age = 0;
public int Age
{
get { return age; }
set { age = value; }
}
public List<Customer> GetCustomerList()
{
List<Customer> listitem = new List<Customer>();
for (int n = 1; n < 245; n++)
{
listitem.Add(new Customer() { Uid = n.ToString(), Name = "name" + n.ToString(), Age = n * 10 });
}
return listitem;
}
}