aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" >
<HeaderTemplate>
<% string str = Request.CurrentExecutionFilePath;%>
<% for (i = 0; i < pages.Length; i++) { %>
<a href="<%=str%>?id=<%=pages[i] %>"><%=pages[i]%></a>
<% }%>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("lname") %>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:DataList></div>
<asp:HyperLink ID="first" runat="server">第一页</asp:HyperLink>
<asp:HyperLink ID="pre" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">最后一页</asp:HyperLink>
</form>
</body>
</html>
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataSet ds;
public int i;
public int[] pages;
PagedDataSource pds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("Server=(local);database=pubs;uid=sa;pwd=");//建立连接
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from employee",conn);//建立适配器
ds = new DataSet();
sda.Fill(ds, "employee");//填充数据
pds = new PagedDataSource();//建立分页块
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true; //启用分页
pds.PageSize = 7; //每页数
int cpage;//当前页
if (Request.QueryString["id"] != null)
{
cpage = Convert.ToInt32(Request.QueryString["id"]);
}
else cpage = 1;
pds.CurrentPageIndex = cpage - 1;
if (!pds.IsFirstPage)
pre.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + Convert.ToInt32(cpage-1);
if (!pds.IsLastPage)
next.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + Convert.ToInt32(cpage + 1);
if (!pds.IsFirstPage)
first.NavigateUrl = Request.CurrentExecutionFilePath + "?id=1";
if (!pds.IsLastPage)
last.NavigateUrl = Request.CurrentExecutionFilePath + "?id="+ Convert.ToInt32(pds.PageCount);
changpage(Convert.ToInt32(Request.QueryString["id"]));
DataList1.DataSource = pds;
DataList1.DataBind();
}
}
//改变数组的值
public void changpage(int ipage)
{
if (pds.PageCount >= 10)
{
if (ipage >= 4)
{
if (pds.PageCount-ipage<10)
{
pages = new int[10];
pages[0] = pds.PageCount-9;
pages[1] = pds.PageCount-8;
pages[2] = pds.PageCount-7;
pages[3] = pds.PageCount-6;
pages[4] = pds.PageCount-5;
pages[5] = pds.PageCount-4;
pages[6] = pds.PageCount-3;
pages[7] = pds.PageCount-2;
pages[8] = pds.PageCount-1;
pages[9] = pds.PageCount;
}
else
{
pages = new int[10];
pages[0] = ipage - 3;
pages[1] = ipage - 2;
pages[2] = ipage - 1;
pages[3] = ipage;
pages[4] = ipage + 1;
pages[5] = ipage + 2;
pages[6] = ipage + 3;
pages[7] = ipage + 4;
pages[8] = ipage + 5;
pages[9] = ipage + 6;
}
}
else
{
pages = new int[10];
pages[0] = 1;
pages[1] = 2;
pages[2] = 3;
pages[3] = 4;
pages[4] = 5;
pages[5] = 6;
pages[6] = 7;
pages[7] = 8;
pages[8] = 9;
pages[9] = 10;
}
}
if (pds.PageCount < 10)
{
pages = new int[pds.PageCount];
for (int i =0; i < pds.PageCount; i++)
{
pages[i] = i+1;
}
}
}
}