datalist實現分頁

 原文件test.aspx代碼:

 

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

<!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">
    <table border="0" cellpadding="0" cellspacing="0" align="center">
    <tr>
        <td style="text-align: center;">
            價格信息
        </td>
    </tr>
    <tr>
        <td style="text-align: center; height: 139px;">
            <asp:DataList ID="DataList1" runat="server">
                <ItemTemplate>
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="width: 100px; height: 16px">
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("lname") %><%# Bind("fname") %>'>'></asp:Label>
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("premoney") %>'></asp:Label>
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Button ID="btnEdit" runat="server" Text="修改" OnClientClick="return confirm('確認要編輯嗎?');"
                                    CommandName="Edit" CommandArgument=' <%# Eval( "id") %>   ' />
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Button ID="Button2" runat="server" Text="刪除" OnClientClick="return confirm('確認要刪除嗎?');"
                                    CommandArgument='<%#Eval("Id") %>' CommandName="Delete" />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <HeaderTemplate>
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="width: 100px; height: 12px;">
                                編號
                            </td>
                            <td style="width: 100px; height: 12px;">
                                姓名
                            </td>
                            <td style="width: 100px; height: 12px;">
                                預存款
                            </td>
                            <td style="width: 100px; height: 12px;">
                                編輯
                            </td>
                            <td style="width: 100px; height: 12px">
                                刪除
                            </td>
                        </tr>
                    </table>
                </HeaderTemplate>
             </asp:DataList>
            <asp:Label ID="lbCount" runat="server" Text="lbCount"></asp:Label>
            <asp:Label ID="lbTotalPage" runat="server" Text="lbTotalPage"></asp:Label>
            <asp:Label ID="lbCurPage" runat="server" Text="lbCurPage"></asp:Label>
            <asp:HyperLink ID="hlnkHead" runat="server">首頁</asp:HyperLink>
            <asp:HyperLink ID="hlnkPrev" runat="server">上一頁</asp:HyperLink>
            <asp:HyperLink ID="hlnkNext" runat="server">下一頁</asp:HyperLink>
            <asp:HyperLink ID="hlnkEnd" runat="server">尾頁</asp:HyperLink>
         </td>
         </tr>
    </table>      
      
    </form>
</body>
</html>

 

test.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 test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dListBindData();
    }
    private void dListBindData()
    {
        string strCon ="server=.;uid=sa;pwd=sa;database=northwind";
        SqlConnection con = new SqlConnection(strCon);
        SqlDataAdapter da = new SqlDataAdapter("select * from ren", con);
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            //填充數據
            da.Fill(ds, "ren");
            // 創建分頁類
            PagedDataSource objPage = new PagedDataSource();
            objPage.DataSource = ds.Tables["ren"].DefaultView;
            //設置總的查詢結果
            lbCount.Text = "總共有" + ds.Tables["ren"].Rows.Count.ToString()
+ "條記錄";
            //設置可以分頁以及每頁的行數
            objPage.AllowPaging = true;
            objPage.PageSize = 6;
            //總頁數
            lbTotalPage.Text = "總頁數:" + objPage.PageCount.ToString() +
"頁";
            //定義變量來保存當前頁
            int CurPage;
            //判斷是否具有頁面跳轉的請求
            if (Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            //設置當前頁的索引
            objPage.CurrentPageIndex = CurPage - 1;
            lbCurPage.Text = "當前頁 : 第" + CurPage.ToString() + "頁";
            //如果不是首頁
            if (!objPage.IsFirstPage)
            {
                //定義上一頁超鏈接的url爲: 當前執行頁面的虛擬路徑,並傳遞下一頁面的索引值
                hlnkHead.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + Convert.ToString(1);
                hlnkPrev.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + Convert.ToString(CurPage - 1);
            }
            //如果不是最後一頁
            if (!objPage.IsLastPage)
            {
                //定義“下一頁”超鏈接url爲:當前執行頁面的虛擬路徑,並傳遞下一頁面的索引值
                hlnkNext.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + Convert.ToString(CurPage + 1);
                hlnkEnd.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + objPage.PageCount.ToString();
            }
            DataList1.DataSource = objPage;
            DataList1.DataBind();
            con.Close();
        }
        catch (Exception error)
        {
            Response.Write(error.ToString());
        }
    }

}

發佈了16 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章