.NET2.0DataList分页

今天做了一个简单的在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;
        }
     
    }
    /// <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();
    }

前台:

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

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

<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语句换成存储过程,也很容易实现。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值