<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
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("Fist", 1, pageUrl));
strPager.Append(string.Format("Previous", (currentPage - 2) + 1,
pageUrl));
}
if(!(currentPage > 1))
{
strPager.Append("FirstPrevious");
}
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("{0} ", n, (n - 1) + 1,pageUrl));
}
if (currentPage < totalPages)
{
strPager.Append(String.Format("Next ", currentPage + 1, pageUrl));
strPager.Append(String.Format("Last ", totalPages, pageUrl));
}
if (!(currentPage < totalPages))
{
strPager.Append("NextLast");
}
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;
}
}