制作分页
我的数据库中有8条记录
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataListDaTabind();
}
}
protected void DataListDaTabind()
{
AccessDataSource ads = new AccessDataSource();
ads.DataFile = "~/App_Data/shopcart.mdb";
ads.SelectCommand = "select * from Product order by productid ASC";
DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = dv;
objPage.AllowPaging = true;
objPage.PageSize =6;
int TolPage;
TolPage = objPage.PageCount;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
lblCurPage.Text = "目前在第" + CurPage.ToString() + "页,共" + TolPage.ToString() + "页";
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TolPage.ToString();
if (!objPage.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
lnkPrev.Visible = false;
lnkFirst.Visible = false;
}
if (!objPage.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
lnkNext.Visible = false;
lnkLast.Visible = false;
}
DataList1.DataSource = objPage;
DataList1.DataBind();//数据库中有8条记录,运行后共两页当单击下一页时总在这提示错误“索引 -6 不是为负数,就是大于行数”。
求救!!!!
------解决方案--------------------
分页在SQL里面去分 不要这么用。。。
C# code
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
public class PagingCollection<T>
{
private int pagezise;
public int Pagezise
{
get { return pagezise; }
set { pagezise = value; }
}
private int pageindex;
public int Pageindex
{
get { return pageindex; }
set { pageindex = value; }
}
private int TotalCount;
public int TotalCount1
{
get { return TotalCount; }
set { TotalCount = value; }
}
private int TotalPage=0;
public int TotalPage1
{
get { return TotalPage; }
set { TotalPage = value; }
}
private string url;
public string Url
{
get { return url; }
set { url = value; }
}
private string urlparms;
public string Urlparms
{
get {
return urlparms; }
set { urlparms = value; }
}
/// <summary>
/// 分页数据构造
/// </summary>
///