一,引入控件(AspNetPager.dll)
二,写方法
/// <summary>
/// 完美的ASP.NET页面分页控件
/// </summary>
/// <param name="PageSize">每页显示的条数</param>
/// <param name="PageIndex">当前页</param>
/// <param name="DoCount">是否显示总条数</param>
/// <param name="RowCount">总条数</param>
/// <param name="TableName">要显示的数据表名</param>
/// <param name="PID">数据表的主键</param>
/// <param name="ColList">显示的字段名</param>
/// <param name="Condition">条件,主要是WHERE</param>
/// <param name="OrderBy">排序</param>
/// <returns>DataSet数据集</returns>
public DataSet DoPageList(int PageSize, int PageIndex, bool DoCount, out int RowCount,
string TableName, string PID, string ColList, string Condition, string OrderBy)
{
if (ColList == "" || ColList == null) ColList = "*";
if (Condition == null || Condition == "") Condition = "";
if (OrderBy == null || OrderBy == "") OrderBy = "";
string strSQL = "select * from (select ROWNUM R,A.* from ( select " + ColList + " " + " from " + TableName + " " + Condition + " " + OrderBy + ") A) where R > " + (PageIndex - 1) * PageSize + " and R <=" + PageIndex * PageSize;
if (DoCount)
{
string sqlcount = "select count(1) from " + TableName + " " + Condition + " " + OrderBy;
object obj = DbFactory.Instance.GetDbHelp().ExecuteScalar(sqlcount);
if (obj != null)
{
RowCount = int.Parse(obj.ToString());
}
else
{
RowCount = 0;
}
}
else
{
RowCount = 0;
}
return DbFactory.Instance.GetDbHelp().GetDataSet(strSQL);
}
三.引用方法
int RowCount = 0;
DataSet ds = new DataSet();
ds = DoPageList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, true, out RowCount, "codes", "", "", "", "");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
AspNetPager1.RecordCount = RowCount;
AspNetPager1.CustomInfoHTML = " <font color=\"blue\"><b>共有:" + RowCount + "条信息,总页数:" + AspNetPager1.PageCount + ",当前位置:第" + AspNetPager1.CurrentPageIndex + "页</b></font>";
ds.Dispose();