在ASP.NET中分頁顯示DataList控件中的數據

效果圖:
在這裏插入圖片描述
所用的數據庫是這個樣子的:
在這裏插入圖片描述代碼,分別是.aspx文件和.aspx.cs文件,自行復制粘貼使用,注意如果要匹配自己的數據庫就要修改數據庫連接以及字段名:

Default.aspx

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

<!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>
            <table border="0" cellpadding="0" cellspacing="0" style="width: 653px">
                <tr>
                  <td align="left">
                        <asp:DataList ID="DataList1" runat="server" Width="693px" 
                            style="font-size: small" onitemcommand="DataList1_ItemCommand" 
                            onitemdatabound="DataList1_ItemDataBound" DataKeyField="book_code">
                            
                            <ItemTemplate>
                                <table>
                                    <tr style="border-bottom-style: groove; border-bottom-width: medium; border-bottom-color: #FFFFFF">
                                        <td rowspan="3" align="center" class="style3">
                                            <a href='#'><img border="0" height="80" src='images/showimg.gif' width="80" alt=""></img></a>
                                        </td>
                                        <td align="left">
                                            <asp:Image ID="Image4" runat="server" ImageUrl="~/images/ico2.gif" />
                                            <a><%#Eval("book_name")%></a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">圖書編碼:<a><%#Eval("book_code") %></a></td>
                                    </tr>
                                </table>
                            </ItemTemplate>

                            
                            <FooterTemplate>
                                <div style="text-align: center">
                                    <table id="Page" border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; width: 68%">
                                        <tr>
                                            <td >
                                                <asp:Label ID="labCurrentPage" runat="server"></asp:Label>/
                                                <asp:Label ID="labPageCount" runat="server"></asp:Label>
                                                <asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first" Font-Underline="False" ForeColor="Black">首頁</asp:LinkButton>
                                                <asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre" Font-Underline="False" ForeColor="Black">上一頁</asp:LinkButton> 
                                                <asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next" Font-Underline="False" ForeColor="Black">下一頁</asp:LinkButton>
                                                <asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last" Font-Underline="False" ForeColor="Black">尾頁</asp:LinkButton>
                                                <asp:Label ID="Label1" runat="server" Text="跳轉至:"></asp:Label>
                                                <asp:TextBox ID="txtPage" runat="server" Width="35px" Height="21px"></asp:TextBox>
                                                <asp:Button ID="Button1" runat="server" CommandName="search" Text="GO" Height="19px" />
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </FooterTemplate>
                        </asp:DataList>
		            </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//引入命名空間
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    static PagedDataSource pds = new PagedDataSource();//創建一個分頁數據源的對象且一定要聲明爲靜態
    SqlConnection conn = new SqlConnection(@"Server=DEITIVOD;Database=db_LibraryMS;User Id=sa;pwd=admin");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataList(0);
        }
    }
    private void BindDataList(int currentpage)
    {
        pds.AllowPaging = true;//允許分頁
        pds.PageSize = 3;//每頁顯示3條數據
        pds.CurrentPageIndex = currentpage;//當前頁爲傳入的一個int型值
        string strSql = "SELECT * FROM tb_bookinfo";//定義一條SQL語句
        conn.Open();//打開數據庫連接 
        SqlDataAdapter sda = new SqlDataAdapter(strSql,conn);
        DataSet ds = new DataSet();
        sda.Fill(ds);//把執行得到的數據放在數據集中
        pds.DataSource = ds.Tables[0].DefaultView;//把數據集中的數據放入分頁數據源中
        DataList1.DataSource = pds;//綁定Datalist
        DataList1.DataBind();
        conn.Close();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            //以下5個爲 捕獲用戶點擊 上一頁 下一頁等時發生的事件
            case "first"://第一頁
                pds.CurrentPageIndex = 0;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "pre"://上一頁
                pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "next"://下一頁
                pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "last"://最後一頁
                pds.CurrentPageIndex = pds.PageCount - 1;
                BindDataList(pds.CurrentPageIndex);
                break;
            case "search"://頁面跳轉頁
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    int PageCount = int.Parse(pds.PageCount.ToString());
                    TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
                    int MyPageNum = 0;
                    if (!txtPage.Text.Equals(""))
                        MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
                    if (MyPageNum <= 0 || MyPageNum > PageCount)
                        Response.Write("<script>alert('請輸入頁數並確定沒有超出總頁數!')</script>");
                    else
                        BindDataList(MyPageNum - 1);
                }
                break;
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            //以下六個爲得到腳模板中的控件,並創建變量.
            Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
            Label PageCount = e.Item.FindControl("labPageCount") as Label;
            LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
            LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
            LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
            LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
            CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//綁定顯示當前頁
            PageCount.Text = pds.PageCount.ToString();//綁定顯示總頁數
            if (pds.IsFirstPage)//如果是第一頁,首頁和上一頁不能用
            {
                FirstPage.Enabled = false;
                PrePage.Enabled = false;
            }
            if (pds.IsLastPage)//如果是最後一頁"下一頁"和"尾頁"按鈕不能用
            {
                NextPage.Enabled = false;
                LastPage.Enabled = false;
            }
        }
    }
}

以上代碼使用的圖片文件在這個鏈接中可以下載
提取碼:fs0i

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章