自己用的一个分页类,改改吧。。
using System;
using System.Data;
using System.Configuration;
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.Text;
/// <summary>
/// pager 的摘要说明
/// </summary>
public class Pager
{
private System.Web.UI.WebControls.DataList datalist = null;//这里改改
PagedDataSource objPds = new PagedDataSource();
StringBuilder sb = new StringBuilder();
public Pager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public System.Web.UI.WebControls.DataList Datalist //这里改改
{
get
{
return this.datalist;
}
set
{
this.datalist = value;
}
}
public string PagerHtml
{
get
{
if (datalist.Items.Count > 0)
{
return sb.ToString();
}
else
{
return null;
}
}
}
//总数据数
public int RecordCount
{
get
{
return objPds.DataSourceCount;
}
}
//总页数
public int PageCount
{
get
{
return objPds.PageCount;
}
}
public void Pagination(DataSet ds, int pageNum)
{
objPds.AllowPaging = true;
//每页显示数目
objPds.PageSize = pageNum;
//指定数据源
objPds.DataSource = ds.Tables[0].DefaultView;
int CurPage = 0;//当前页
int PageCount = objPds.PageCount;//总页数
//确定当前页数
string pageName = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page={0}";
if (System.Web.HttpContext.Current.Request.Params["Page"] != null)
{
string str=System.Web.HttpContext.Current.Request.Params["Page"];
if (MyWeb.Common.StringUnit.IsNum(str))
{
CurPage = int.Parse(str);
}
else
{
CurPage = 1;
}
}
else
{
CurPage = 1;
}
objPds.CurrentPageIndex = CurPage-1;
//首页
//上一页
if (CurPage == 1)
{
sb.Append("<IMG src=img/nav_prev_.gif width=12 height=11 border=0 align=absMiddle>");
sb.Append(" <IMG src=img/nav_prev.gif width=12 height=11 border=0 align=absMiddle>");
}
else
{
sb.Append(string.Format("<a title=首页 href={0}><IMG src=img/nav_prev_.gif width=12 height=11 border=0 align=absMiddle></a>", string.Format(pageName, 1)));//首页
sb.Append(string.Format(" <a title=上一页 href={0}><IMG src=img/nav_prev.gif width=12 height=11 border=0 align=absMiddle></a>", string.Format(pageName, CurPage - 1)));//上一页
}
//开始读取的页码
int startPage;
if (CurPage < 10)
{
startPage = 1;
}
else
{
startPage = (CurPage / 10) * 10;
}
//结束页码
int endPage;
if ((CurPage > PageCount))
{
endPage = PageCount;
}
else
{
endPage = (CurPage/10)*10;
if (endPage + 10 > PageCount)
{
endPage = PageCount;
}
else
{
endPage = endPage + 10;
}
}
//开始输出页码到html
for (int i = startPage; i <= endPage; i++)
{
if (CurPage == i)
{
sb.Append(string.Format(" <FONT class=p_tx11b>{0}</FONT>", i));
if (i < endPage)
{
sb.Append(" <FONT color=#cccccc size=1>l</FONT>");
}
}
else
{
sb.Append(string.Format(" <A class=b_tx11b title=第{1}页 href={0}>{1}</A>", string.Format(pageName, i), i));
if (i < endPage)
{
sb.Append(" <FONT color=#cccccc size=1>l</FONT>");
}
}
}
//下一页
//后十页
if (CurPage == objPds.PageCount)
{
sb.Append(" <IMG src=img/nav_next.gif width=12 height=11 border=0 align=absMiddle>");
sb.Append(" <IMG src=img/nav_next_.gif width=12 height=11 border=0 align=absMiddle>");
}
else
{
sb.Append(string.Format(" <a title=下一页 href={0}><IMG src=img/nav_next.gif width=12 height=11 border=0 align=absMiddle></a>", string.Format(pageName, CurPage + 1)));//下一页
sb.Append(string.Format(" <a title=最后一页 href={0}><IMG src=img/nav_next_.gif width=12 height=11 border=0 align=absMiddle></a>", string.Format(pageName, PageCount)));//最后一页
}
//绑定datalist
if (this.datalist != null)//这里改改
{
datalist.DataSource = objPds;
datalist.DataBind();
}
}
}