<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="第二章_datalist" Debug="true"%>
<!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:Label ID="lblRecordCount" ForeColor="red" runat="server"></asp:Label>條記錄
當前爲<asp:Label ID="lblCurrentPage" ForeColor="red" runat="server"></asp:Label>/
<asp:Label ID="lblPageCount" ForeColor="red" runat="server"></asp:Label>頁
<asp:DataList ID="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro" EditItemStyle-BackColor="yellow" RepeatColumns="3" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Label ID="bumen" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.neirong") %>'></asp:Label> |
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="lbnPrevPage" Text="上一頁" CommandName="prev" runat="server" OnCommand="Page_OnClick"></asp:LinkButton>
<asp:LinkButton ID="lbnNextPage" Text="下一頁" CommandName="next" runat="server" OnCommand="Page_OnClick"></asp:LinkButton>
</div>
</form>
</body>
</html>
後臺
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.OleDb;
public partial class 第二章_datalist : System.Web.UI.Page
{
OleDbConnection MyConn;//與數據源建立連接
int PageSize, RecordCount, PageCount, CurrentPage;//一頁大小,記錄總數,頁總數,當前頁數
public void Page_Load(Object src, EventArgs e)
{
//設定PageSize
PageSize = 3;
//數據庫連接語句
string MyConnString = "Provider=SQLOLEDB.1;SERVER=.;DATABASE=md_yinhang;UID=sa;PWD=mdsoft;Max Pool Size = 512";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
//第一次請求執行
if (!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//ViewState是一個名稱/值的對象集合。當請求某個頁面時,ASP.NET會把所有控件的狀//態序列化成一個字符串,然後作爲窗體的隱藏屬性送到客戶端,當客戶端吧頁面回傳時,//ASP.NET分析回傳的窗體屬性,並賦給控件對應的值。
//計算總共有多少記錄
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//計算總共有多少頁
PageCount1 = RecordCount/ PageSize;//整除求得
PageCount2 = RecordCount % PageSize;//取模
//當不是整除的時候,在總頁數上要再加一
if (PageCount2 >= 1 && PageCount2 < PageSize)
{
PageCount = PageCount1 + 1;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
//如果是整除的話,就直接顯示頁數
else
{
PageCount = PageCount1;//當整除情況下,總頁數就是等於被除數
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//計算總共有多少條記錄
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from bumen";
OleDbCommand MyComm = new OleDbCommand(strCount, MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//設定導入的起終地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from bumen";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds,StartIndex, PageSize, "bumen");
return ds.Tables["bumen"].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)
{
ListBind();
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();
}
}