asp.net DataList控制元件分頁程式碼

暖楓無敵發表於2011-11-18
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="TEST_DataList" %>

<!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="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro"
            EditItemStyle-BackColor="yellow">
            <HeaderTemplate>
                <table width="100%" border="1">
                    <tr>
                        <td align="center">
                            使用者ID
                        </td>
                        <td align="center">
                            使用者名稱
                        </td>
                        <td align="center">
                            密 碼
                        </td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td align="center">
                        <%# DataBinder.Eval(Container.DataItem,"使用者ID") %>
                    </td>
                    <td align="center">
                        <%# DataBinder.Eval(Container.DataItem,"使用者名稱") %>
                    </td>
                    <td align="center">
                        <%# DataBinder.Eval(Container.DataItem,"密碼") %>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:DataList>
        <asp:LinkButton ID="lbnPrevPage" Text="上一頁" CommandName="prev" OnCommand="Page_OnClick"
            runat="server" />
        <asp:LinkButton ID="lbnNextPage" Text="下一頁" CommandName="next" OnCommand="Page_OnClick"
            runat="server" />
        共有<asp:Label ID="lblRecordCount" ForeColor="red" runat="server" />條記錄 當前為<asp:Label
            ID="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label ID="lblPageCount"
                ForeColor="red" runat="server" />頁
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

public partial class TEST_DataList : System.Web.UI.Page
{
    //分頁相關引數
    int PageSize, RecordCount, PageCount, CurrentPage;
    SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=CSDN;Persist Security Info=True;User ID=sa;Password=sa;");
    protected void Page_Load(object sender, EventArgs e)
    {
        //設定PageSize
        PageSize = 10;
        if (!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 SYS_USERINF";
        con.Open();
        SqlCommand cmd = new SqlCommand(strCount, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            intCount = Int32.Parse(dr["co"].ToString());
        }
        else
        {
            intCount = 0;
        }
        dr.Close();
        con.Close();
        return intCount;
    }

    ICollection CreateSource()
    {
        int StartIndex;
        //設定匯入的起終地址
        StartIndex = CurrentPage * PageSize;
        string strSel = "select * from SYS_USERINF";
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(strSel, con);
        sda.Fill(ds, StartIndex, PageSize, "Score");
        return ds.Tables["Score"].DefaultView;
    }

    public void ListBind()
    {
        score.DataSource = CreateSource();
        score.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();
    }
}

樣式沒有應用好的css,比較難看,但是分頁功能就是如此實現的。上圖:



相關文章