利用DataSet分页方法 小宝马的爸爸

***********************************
     利用DataSet分页方法
***********************************

主要利用DataSet的筛选数据的方法
DataAdapter用Fill方法填充DataSet的时候把所取的整个记录加入到DataSet
例如:
    MyAdapter.Fill(DataSet,TableName);
有时候并不需要将整个查询数据都导入DataSet,真正需要的只是数据的一部。Fill的另一种方法可以满足这种需要。
    MyAdapter.Fill(DataSet,StartRow,RowsCount,TableName);
可以看到在这种使用方法中有多出了两个整型参数StartRow与RowCout,他们标识将数据源中从StartRow位置取出RowsCount条记录导入DataSet。得到数据后,程序还将DataSet中的数据显示出来。

代码如下:

前台:fenye.aspx

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

<!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>DataList分页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="book" runat="server">
        <ItemTemplate>
            编号:<%#DataBinder.Eval(Container.DataItem,"book") %>
        </ItemTemplate>
        </asp:DataList></div>
        共有<asp:Label ID="lblRecordCount" runat="server"></asp:Label>条记录<br />
        当前为<asp:Label ID="lblCurrentPage" runat="server"/>/<asp:Label ID="lblPageCount" runat="server" Text="Label"/>页&nbsp;
        <br />
        <asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick">上一页</asp:LinkButton>
        <asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton>
    </form>
</body>
</html>

 

后台:fenye.aspx.cs

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 fenye : System.Web.UI.Page
{
    SqlConnection sqlcon;
    int PageSize, RecordCount, PageCount, CurrentPage;
    public void Page_Load(object sender, EventArgs e)
    {
        PageSize = 3;
        sqlcon = new SqlConnection();
        sqlcon.ConnectionString = "Data Source=localhost;Initial Catalog=test;User Id=sa;Password=guest";
        sqlcon.Open();
        //第一次请求执行
        if (!Page.IsPostBack)
        {
            ListBind();
            CurrentPage = 0;
            ViewState["PageIndex"] = 0;

            //计算总共有多少条记录
            RecordCount = CalculateRecord();
            lblRecordCount.Text = RecordCount.ToString();

            //计算总共多少页
            PageCount = RecordCount / PageSize;
            lblPageCount.Text = PageCount.ToString();
            ViewState["PageCount"] = PageCount;

         }
    }
        //计算总共有多少条记录
    public int CalculateRecord()
    {
        int intCount;
        string strCount = "select count(*) as co from book";
        SqlCommand sqlcmd = new SqlCommand(strCount,sqlcon);
        SqlDataReader sdr = sqlcmd.ExecuteReader();
        if (sdr.Read())
        {
            intCount = Int32.Parse(sdr["co"].ToString());
        }
        else
        {
            intCount = 0;
        }
        sdr.Close();
        return intCount;
    }
    ICollection CreateSource()  //ICollection为何使用?
    {
        int StartIndex;

        //设定导入的起终地址
        StartIndex = CurrentPage * PageSize;
        string strSel = "select * from book";
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(strSel, sqlcon);
        sda.Fill(ds,StartIndex,PageSize,"book");//分页的关键所在,该句表示将数据源中从StartIndex位置取出PageSize条记录导入DataSet.
        return ds.Tables["book"].DefaultView;
    }
    public void ListBind()
    {
        book.DataSource = CreateSource();
        book.DataBind();
        lbnNextPage.Enabled = true;
        lbnPrevPage.Enabled = true;
        if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
        if (CurrentPage == 0) lbnPrevPage.Enabled = false;
        lblCurrentPage.Text = (CurrentPage + 1).ToString();
    }
    public void Page_OnClick(Object sender, CommandEventArgs e)
    {
        CurrentPage=(int)ViewState["PageIndex"];
        PageCount=(int)ViewState["PageCount"];
        string cmd = e.CommandName;
        //判断cmd,以判定翻页方向
        switch (cmd)
        {
            case "next":
                if(CurrentPage<(PageCount-1))  CurrentPage++;
                break;
            case "prev":
                if (CurrentPage > 0) CurrentPage--;
                break;
        }
        ViewState["PageIndex"]=CurrentPage;
        ListBind();
    }      
}

分页的方式又很多,可以用PagedDataSource类,还可以只用数据库进行分页,我展现的这个分页方法一是它巧妙地利用了DataSet的一个方法,另外算法也很清晰,所以自己比较喜欢。编程是一个探究的过程,可能还有很多好的方法,我也没能看到想到,路过的同仁如果有好的方法敬请赐教!


 

原文地址:https://www.cnblogs.com/gisera/p/1282355.html