Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/24 9:02:04 .NET2.0DataList分页_[Asp.Net教程]
今天做了一个简单的在DataList中分页的小东西.用到的核心的东西就是PageDataSource类.和repeater的分页思想是一样的。
代码如下:
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 System.Data.SqlClient;public partial class DataListPage : System.Web.UI.Page{ private const int PAGESIZE = 3;//声明每一页包含的记录数 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.DataListBind(this.getDataView()); int currentPageIndex = 1;//指定当前页 ViewState["currentPageIndex"] = currentPageIndex; } } /// /// 向后 /// /// /// protected void Button1_Click(object sender, EventArgs e) { //首先判断显示的记录数是否已经超过了所有的记录数 if ((int)ViewState["currentPageIndex"] * PAGESIZE < this.CaculateRecordCount()) { int recordIndex = ((int)ViewState["currentPageIndex"]) * PAGESIZE;//计算当前页要显示的记录的索引 this.DataListBind(this.getDataView(recordIndex, PAGESIZE));//绑定到DATALIST ViewState["currentPageIndex"] = (int)ViewState["currentPageIndex"] + 1;//当前页加1 } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "kk", "alert('到底最后一页')", true); } } /// /// 返回记录的总数目 /// /// 记录的总条数 private int CaculateRecordCount() { SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa"); conn.Open(); //计算所有的记录数 SqlDataAdapter da = new SqlDataAdapter("select count(*) as rc from region", conn); int rc = (int)da.SelectCommand.ExecuteScalar(); conn.Close(); return rc; } /// /// 向前 /// /// /// protected void Button2_Click(object sender, EventArgs e) { if ((int)ViewState["currentPageIndex"] > 1) { int recordIndex = ((int)ViewState["currentPageIndex"] - 2) * PAGESIZE;//计算当前也要显示的首记录的索引 this.DataListBind(this.getDataView(recordIndex, PAGESIZE));//绑定 ViewState["currentPageIndex"] = (int)ViewState["currentPageIndex"] - 1;//当前页码减1 } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "kk", "alert('到达第一页!')",true ); } } /// /// 页面刚加载时产生绑定视图 /// /// private DataView getDataView() { SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa"); SqlDataAdapter da = new SqlDataAdapter("select * from region", conn); DataSet ds = new DataSet(); da.Fill(ds); return ds.Tables[0].DefaultView; } /// /// 翻页的时候产生绑定视图 /// /// 该也要加载的首记录的索引 /// 页面的记录大小 /// private DataView getDataView(int recordIndex, int pageSize) { SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa"); SqlDataAdapter da = new SqlDataAdapter("select * from region", conn); DataSet ds = new DataSet(); da.Fill(ds, recordIndex, pageSize, "tt");//将首记录索引为recordIndex开始的pagesize条记录填充到数据集 return ds.Tables["tt"].DefaultView; } /// /// 绑定datalist /// /// 数据源 private void DataListBind(DataView dv) { PagedDataSource pds = new PagedDataSource(); pds.PageSize = PAGESIZE; pds.AllowPaging = true; pds.DataSource = dv; this.DataList1.DataSource = pds; this.DataList1.DataBind(); }}
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListPage.aspx.cs" Inherits="DataListPage" %>
赞