.NET2.0DataList分頁

來源:互聯網
上載者:User

今天做了一個簡單的在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語句換成預存程序,也很容易實現。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.