代码一:
PagedDataSource ps = new PagedDataSource();
ps.DataSource = dt.DefaultView;
ps.AllowPaging = true;
ps.PageSize = 10;
int pagecount;
if (Request.QueryString["page"] != null)
pagecount = Convert.ToInt32(Request.QueryString["page"].ToString());
else
pagecount = 1;
ps.CurrentPageIndex = pagecount - 1;
lblPagecount.Text = ps.PageCount.ToString();
lblList.Text = "";
int MinPage = pagecount - 5;
int MaxPage = pagecount + 5;
if(MaxPage>ps.PageCount)
MaxPage=ps.PageCount;
if (MinPage < 0)
MinPage = 0;
for (int i = MinPage; i < MaxPage; i++)
{
string url = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(i+1);
string pg = Convert.ToString(i + 1);
if (i == pagecount-1)
pg = "<Font color=red>" + pg + "</font>";
lblList.Text += "<a href=" + url + " >[ " + pg + " ]</a> ";
}
lblList.Text += "...";
//Response.Write(ts.Rows[0][0]);
if (!ps.IsFirstPage)
this.hplFist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
if (!ps.IsFirstPage)
this.hplPrv.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pagecount - 1);
if (!ps.IsLastPage)
this.hplNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pagecount + 1);
if (!ps.IsLastPage)
this.hplLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(ps.PageCount);
rep_Userans.DataSource = ps;
rep_Userans.DataBind();
代码二:
//ASP.NET2.0中datalist仿百度分页
//注意这个分页在1.0中无效
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.IO;
public partial class mysql : System.Web.UI.Page
{
}
//需要在前台添加一个