<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SplitPage.aspx.cs" Inherits="SplitPage" %>
<!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>
<title>分页算法</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataList ID="datalist1" AlternatingItemStyle-BackColor="#f3f3f3" Width="100%"
CellSpacing="0" CellPadding="0" runat="server">
<ItemTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%" align="center">
<%#DataBinder.Eval(Container.DataItem,"id")%>
</td>
<td width="30%" align="center">
<%#DataBinder.Eval(Container.DataItem, "errormessage")%>
</td>
<td width="30%" align="center">
<%#DataBinder.Eval(Container.DataItem, "type")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<div align="center">
共<asp:Label ID="LPageCount" runat="server" ForeColor="#ff0000"></asp:Label>页/共
<asp:Label ID="LRecordCount" runat="server" ForeColor="#ff0000"></asp:Label>记录
<asp:LinkButton ID="Fistpage" runat="server" CommandName="0" OnCommand="Page_OnClick">首页</asp:LinkButton> <asp:LinkButton
ID="Prevpage" runat="server" CommandName="prev" OnCommand="Page_OnClick">
上一页</asp:LinkButton> <asp:LinkButton ID="Nextpage" runat="server"
CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton> <asp:LinkButton
ID="Lastpage" runat="server" CommandName="last" OnCommand="Page_OnClick">尾页</asp:LinkButton> 当前第<asp:Label
ID="LCurrentPage" runat="server" ForeColor="#ff0000"></asp:Label>页 <br>
<asp:TextBox ID="txtGoto" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="Goto" OnClick="LinkButton5_Click">跳转</asp:LinkButton></div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
public partial class SplitPage : System.Web.UI.Page
{
const int PageSize = 10;//定义每页显示记录
int PageCount, RecCount, CurrentPage, Pages, JumpPage;
protected System.Web.UI.WebControls.LinkButton lkbGoto;//定义几个保存分页参数变量
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RecCount = Calc();//通过Calc()函数获取总记录数
PageCount = RecCount / PageSize + OverPage();
ViewState["PageCounts"] = RecCount / PageSize - ModPage();//保存总页参数到ViewState(减去ModPage()函数防止SQL语句执行时溢出查询范围,可以用存储过程分页算法来理解这句)
ViewState["PageIndex"] = 0;//保存一个为0的页面索引值到ViewState
ViewState["JumpPages"] = PageCount;//保存PageCount到ViewState,跳页时判断用户输入数是否超出页码范围
//显示LPageCount、LRecordCount的状态
LPageCount.Text = PageCount.ToString();
LRecordCount.Text = RecCount.ToString();
//判断跳页文本框失效
if (RecCount <= 20)
txtGoto.Enabled = false;
TDataBind();
}
}
//计算余页
public int OverPage()
{
int pages = 0;
if (RecCount % PageSize != 0)
pages = 1;
else
pages = 0;
return pages;
}
//计算余页,防止SQL语句执行时溢出查询范围
public int ModPage()
{
int pages = 0;
if (RecCount % PageSize == 0 && RecCount != 0)
pages = 1;
else
pages = 0;
return pages;
}
public static int Calc()
{
int RecordCount = 0;
SqlCommand MyCmd = new SqlCommand("select count(*) as co from failurelog ", MyCon());
SqlDataReader dr = MyCmd.ExecuteReader();
if (dr.Read())
RecordCount = Int32.Parse(dr["co"].ToString());
MyCmd.Connection.Close();
return RecordCount;
}
public static SqlConnection MyCon()
{
SqlConnection MyConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
MyConnection.Open();
return MyConnection;
}
protected void Page_OnClick(object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数运算
string cmd = e.CommandName;
switch (cmd)
{
case "next":
CurrentPage++;
break;
case "prev":
CurrentPage--;
break;
case "last":
CurrentPage = Pages;
break;
default:
CurrentPage = 0;
break;
}
ViewState["PageIndex"] = CurrentPage;//将运算后的CurrentPage变量再次保存至ViewState
TDataBind();//调用数据绑定函数TDataBind()
}
private void TDataBind()
{
CurrentPage = (int)ViewState["PageIndex"];//从ViewState中读取页码值保存到CurrentPage变量中进行按钮失 效运算
Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数进行按钮失效运算
//判断四个按钮(首页、上一页、下一页、尾页)状态
if (CurrentPage + 1 > 1)
{
Fistpage.Enabled = true;
Prevpage.Enabled = true;
}
else
{
Fistpage.Enabled = false;
Prevpage.Enabled = false;
}
if (CurrentPage == Pages)
{
Nextpage.Enabled = false;
Lastpage.Enabled = false;
}
else
{
Nextpage.Enabled = true;
Lastpage.Enabled = true;
}
//数据绑定到DataList控件
DataSet ds = new DataSet();
//核心SQL语句,进行查询运算(决定了分页的效率:))
SqlDataAdapter MyAdapter = new SqlDataAdapter("Select Top " + PageSize + " * from failurelog where id not in(select top " + PageSize * CurrentPage + " id from failurelog order by id asc) order by id asc", MyCon());
MyAdapter.Fill(ds, "news");
datalist1.DataSource = ds.Tables["news"].DefaultView;
datalist1.DataBind();
//显示Label控件LCurrentPaget和文本框控件gotoPage状态
LCurrentPage.Text = (CurrentPage + 1).ToString();
txtGoto.Text = (CurrentPage + 1).ToString();
//释放SqlDataAdapter
MyAdapter.Dispose();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
try
{
JumpPage = (int)ViewState["JumpPages"];
//判断用户输入值是否超过可用页数范围值
if (Int32.Parse(this.txtGoto.Text) > JumpPage || Int32.Parse(txtGoto.Text) <= 0)
Response.Write("<script>alert('页码范围越界!');location.href='SplitPage.aspx'</script>");
else
{
int InputPage = Int32.Parse(txtGoto.Text.ToString()) - 1;//转换用户输入值保存在int型InputPage变量中
ViewState["PageIndex"] = InputPage;//写入InputPage值到ViewState["PageIndex"]中
TDataBind();//调用数据绑定函数TDataBind()再次进行数据绑定运算
}
}
//捕获异常
catch (Exception exp)
{
Response.Write("<script>alert('" + exp.Message + "');location.href='SplitPage.aspx'</script>");
}
}
}