使用ASP.NET开发web程序时,经常会遇到数据列表页需要分页的问题。在ASP.NET里,分页可以通过绑定数据控件实现,如GridView、DataList、Repeater、ListView等这些数据控件都可以实现分页效果。它们之间的一些对比:
注意,使用这些控件的时候,ViewState功能必须打开(即 EnableViewState="true" 在NET里,默认是打开的),因为如果关闭了ViewState视图状态,在点击下一页这些跳转按钮时,.net页面是记不住当前状态的。
<a href="javascript:__doPostBack('GridView1','Page$2')" style="color:#333333;">第2页</a>
而我们知道,作为web网站程序开发,搜索引擎对页面的(前台)js的链接并不敏感,也不会抓取和索引js里的链接,这就会造成一个严重的情况,那就是列表页从第二页开始,都无法抓取并索引,大量网页无法出现在搜索引擎的结果页里。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default: System.Web.UI.Page
{
comClass CC = new comClass();//实例化
protected void Page_Load(object sender, EventArgs e)
{//初始化
if (!IsPostBack)
{
string html = getListData();
divlist.InnerHtml = html;
}
}
public static bool IsNum(String str)
{//判断是否是ASCII纯数字
if (str == null || str == "") return false;
for (int i = 0; i < str.Length; i++)
{
if (str[i] < '0' || str[i] > '9')
return false;
}
return true;
}
public string getListData()
{//绑定数据
string html = "";
SqlConnection conn = CC.getConn();
conn.Open();
SqlCommand cmd;
SqlDataReader dr;
try
{
int page = 1;
string page_get = Request["page"]; //获取当前页
if (IsNum(page_get)) page = Convert.ToInt32(page_get);
int pagesize = 5; //每页多少个
int TotalRecords, TotalPages;
//总记录数
cmd = new SqlCommand("select count(*) from data ", conn);
dr = cmd.ExecuteReader();
dr.Read();
TotalRecords = Int32.Parse(dr[0].ToString());
dr.Close();
cmd.Dispose();
//总页数
if (TotalRecords % pagesize == 0) TotalPages = TotalRecords / pagesize;
else TotalPages = TotalRecords / pagesize + 1;
//当前页
if (page < 1) page = 1;
if (page > TotalPages) page = TotalPages;
//---核心分页算法---
string sql = "select top " + pagesize + " * from data order by id desc";
if (page > 1) sql = "select top " + pagesize + " * from data where id<(SELECT Min(id) FROM (SELECT TOP " + pagesize * (page - 1) + " id FROM data ORDER BY id desc) AS T) order by id desc"; cmd = new SqlCommand(sql, conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
html += dr["id"].ToString() + "、" + dr["title"].ToString() + "<br />";
}
dr.Close();
cmd.Dispose();
//分页代码
string cfile = Request.Path; //当前请求的虚拟路径
string pagestr = "\r\n<div class='page_fenye'><a href='" + cfile + "'>首页</a> ";
for (int i=1; i<=TotalPages ; i++ )
{
if (i == page) pagestr += " <span>" + i + "<span> ";
else
{
if (Math.Abs(page - i) < 8)
{
if (i == 1) pagestr += " <a href='" + cfile + "'>" + i + "</a> ";
else pagestr += " <a href='" + cfile + "?page=" + i + "'>" + i + "</a> ";
}
}
}
pagestr += " <a href='" + cfile + "?page=" + TotalPages + "'>尾页</a> ";
pagestr += " <span>第" + page + "/" + TotalPages + "页, 总共" + TotalRecords + "条</span></div>";
html += pagestr;
}
catch (Exception ex) { html = "异常错误:" + ex.ToString(); }
finally { conn.Close(); }
return html;
}
}