当前位置:首页 > .NET2.0DataList分页

.NET2.0DataList分页

点击次数:1120  更新日期:2010-12-28
\n

今天做了一个简单的在DataList中分页的小东西.用到的核心的东西就是PageDataSource类.和repeater的分页思想是一样的。


\n

代码如下:


\n

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;
}

}
/// <summary>
/// 向后
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
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);
}
}
/// <summary>
/// 返回记录的总数目
/// </summary>
/// <returns>记录的总条数</returns>
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;
}
/// <summary>
/// 向前
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
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 );
}
}
/// <summary>
/// 页面刚加载时产生绑定视图
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 翻页的时候产生绑定视图
/// </summary>
/// <param name=”recordIndex”>该也要加载的首记录的索引</param>
/// <param name=”pageSize”>页面的记录大小</param>
/// <returns></returns>
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;
}
/// <summary>
/// 绑定datalist
/// </summary>
/// <param name=”dv”>数据源</param>
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();
}
}


\n

前台:


\n

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”DataListPage.aspx.cs” Inherits=”DataListPage” %>


\n

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>无标题页</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataList ID=”DataList1″ runat=”server”>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server”><%#DataBinder.Eval(Container .DataItem ,”regionid”) %></asp:Label><br />
<asp:Label ID=”Label2″ runat=”server”><%#DataBinder.Eval(Container .DataItem ,”regiondescription” )%></asp:Label>
</ItemTemplate>
</asp:DataList></div>
<asp:Button ID=”Button2″ runat=”server” onClick=”Button2_Click” Text=”Prev” />
<asp:Button ID=”Button1″ runat=”server” onClick=”Button1_Click” Text=”Next” />
</form>
</body>
</html>
如果SQL语句换成存储过程,也很容易实现。

来源:sdtsfhh的blog

\n