我的分页代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
string PageIndex=Request.QueryString["PageIndex"];
if(PageIndex==null||PageIndex.Length==0)//这种方法判断快一些
{
this.BindData(this.Repeater1,1,5);
this.Label1.Text=this.ViewPage(1,5);
}
else
{
this.BindData(this.Repeater1,Convert.ToInt32(PageIndex),5);
this.Label1.Text=this.ViewPage(Convert.ToInt32(PageIndex),5);
}
}
}
/// <summary>
/// 数据绑定
/// </summary>
/// <param name="rep">Repeater控件</param>
/// <param name="PageIndex">当前页</param>
/// <param name="parpage">每页大小</param>
private void BindData(Repeater rep,int PageIndex,int parpage)
{
GJGK.WEB.Business.Conn conn=new GJGK.WEB.Business.Conn(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
rep.DataSource=conn.Read("tbl_Soft_Softs","*","AddedDate",parpage,PageIndex,0,1,"").Tables[0].DefaultView;
rep.DataBind();
}
/// <summary>
/// 显示分页
/// </summary>
/// <param name="PageIndex">当前页</param>
/// <param name="parpage">每页大小</param>
private string ViewPage(int PageIndex,int parpage)
{
int[] arr=PageSize(parpage);
int totalrecord=arr[0];//总的记录数
int totalpage=arr[1];//总的页数
string str="";
string url="Default.aspx";
if(PageIndex==1)
{
if(totalrecord<parpage)
{
str+=" ";
}
else
{
str+="<span style=\"font-size:12px;color:#cccccc;text-decoration: none;\">首页</span> <span style=\"color:#cccccc;font-size:12px\">上一页</span> <a href=\""+url+"?PageIndex="+(PageIndex+1)+"\" style=\"color:#000000;text-decoration: none;font-size:12px\">下一页</a> <a href=\""+url+"?PageIndex="+totalpage+"\" style=\"font-size:12px;color:#000000;text-decoration: none;\">尾页</a>";
str+=" <span style=\"font-size:12px\">页次:<b><font color=\"red\">"+PageIndex+"</font></b>/<b>"+totalpage+"</b> <b>"+parpage+"</b>条主题/页</span>";
}
}
else if(PageIndex<totalpage)
{
str+="<a href=\""+url+"?PageIndex=1\" style=\"font-size:12px;color:#000000;text-decoration: none;\">首页</a> <a href=\""+url+"?PageIndex="+(PageIndex-1)+"\" style=\"color:#000000;text-decoration: none;font-size:12px\">上一页</a> <a href=\""+url+"?PageIndex="+(PageIndex+1)+"\" style=\"color:#000000;text-decoration: none;font-size:12px\">下一页</a> <a href=\""+url+"?PageIndex="+totalpage+"\" style=\"font-size:12px;color:#000000;text-decoration: none;\">尾页</a>";
str+=" <span style=\"font-size:12px\">页次:<b><font color=\"red\">"+PageIndex+"</font></b>/<b>"+totalpage+"</b> <b>"+parpage+"</b>条主题/页</span>";
}
else if(PageIndex==totalpage)
{
int a=totalrecord%parpage;
if(a==0)
{
a=parpage;
}
str+="<a href=\""+url+"?PageIndex=1\" style=\"font-size:12px;color:#000000;text-decoration: none;\">首页</a> <a href=\""+url+"?PageIndex="+(PageIndex-1)+"\" style=\"color:#000000;text-decoration: none;font-size:12px\">上一页</a> <span style=\"font-size:12px;color:#cccccc;text-decoration: none;\">下一页</span> <span style=\"font-size:12px;color:#cccccc;text-decoration: none;\">尾页</span>";
str+=" <span style=\"font-size:12px\">页次:<b><font color=\"red\">"+PageIndex+"</font></b>/<b>"+totalpage+"</b> <b>"+a+"</b>条主题/页</span>";
}
else
{
str+="<font color=\"red\">当前页没有数据</font>";
}
return str;
}
/// <summary>
/// 返回总的页数总条数数组
/// </summary>
/// <param name="parpage">每一页的大小</param>
/// <returns>返回总的页数</returns>
private int[] PageSize(int parpage)
{
GJGK.WEB.Business.Conn conn=new GJGK.WEB.Business.Conn(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
int totalnum=Convert.ToInt32(conn.Read("tbl_Soft_Softs","*","AddedDate",1,1,1,1,"").Tables[0].Rows[0][0]);
int totalpage=(int)System.Math.Ceiling(totalnum/Convert.ToDouble(parpage));
int[] arr=new int[]{totalnum,totalpage};
return arr;
}