在做项目的时候经常要用到分页
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;
namespace AspNetPage
{
/// <summary>
/// MyAspNetPage 的摘要说明
/// </summary>
public class MyAspNetPage
{
public MyAspNetPage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//DS,rpt,共几页,
public static void RptBind(DataSet ds, Repeater rpt, Literal RecordCount, Literal PageCount, Literal Pageindex, HyperLink FirstPage, HyperLink PrevPage, HyperLink NextPage, HyperLink LastPaeg, Literal Literal1)
{
try
{
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = ds.Tables[0].DefaultView;//ds.Tables[0].DefaultView;
objPage.AllowPaging = true;
objPage.PageSize = 10;
int CurPage;
if (System.Web.HttpContext.Current.Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
rpt.DataSource = objPage;//这里更改控件名称
rpt.DataBind();
RecordCount.Text = objPage.DataSourceCount.ToString();
PageCount.Text = objPage.PageCount.ToString();
Pageindex.Text = CurPage.ToString();
Literal1.Text = PageList(objPage.PageCount, CurPage);
FirstPage.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1";
PrevPage.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + (CurPage - 1);
NextPage.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + (CurPage + 1);
LastPaeg.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + objPage.PageCount.ToString();
if (CurPage <= 1 && objPage.PageCount <= 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
}
if (CurPage <= 1 && objPage.PageCount > 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
}
if (CurPage >= objPage.PageCount)
{
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
}
}
catch (Exception error)
{
System.Web.HttpContext.Current.Response.Write(error.ToString());
}
}
private static string PageList(int Pagecount, int Pageindex)//private string Jump_List(int Pagecount , int Pageindex , long L_Manage)//带参数的传递
{
StringBuilder sb = new StringBuilder();
sb.Append("<select id=/"Page_Jump/" name=/"Page_Jump/" onchange=/"window.location='" + System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '';/">");
for (int i = 1; i <= Pagecount; i++)
{
if (Pageindex == i)
sb.Append("<option value='" + i + "' selected>" + i + "</option>");
else
sb.Append("<option value='" + i + "'>" + i + "</option>");
}
sb.Append("</select>");
return sb.ToString();
}
}
}
--------------------------------------------------------------------------------------------