<%@ 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();
}
}