分页是个大问题,,终于走出了第一步了我,,庆祝庆祝!
文件代码如下:
test3.cs :
using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//和读取web.config有关
using System.Configuration;
using System.Collections.Specialized;
namespace test3
{
public class MyCodeBehind : Page
{
protected Repeater rptNews;
protected Literal ltrLink;
//定义每页显示记录
const int intPageSize=20;
OleDbConnection conConnection;
int intPage;
//检测ID
private void Page_Load(object sender,EventArgs e)
{
//读取web.config文件
NameValueCollection col = ConfigurationSettings.AppSettings;
string strDbPath= col.Get("DbPath");
conConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("" + strDbPath + ""));
if(Request.Params["page"] == null)
{
intPage = 1;
OleDbDataAdapter dadNewsCommand = new OleDbDataAdapter("select top " + intPageSize + " id,title,addtime FROM bbs_topic order by id desc", conConnection);
DataSet dstDataSet = new DataSet();
dadNewsCommand.Fill(dstDataSet, "News");
rptNews.DataSource = dstDataSet.Tables["News"].DefaultView;
rptNews.DataBind();
ltrLink.Text = ShowString();
}
else
{
intPage = Int32.Parse(Request.Params["page"].ToString());
PageDataBind();
ltrLink.Text = ShowString();
}
}
//显示页面信息和链接
public string ShowString()
{
string _strLink = "<div align=center>共 " + PageCount().ToString() + " 页 / 共 " + RecordCount().ToString() + " 记录 ";
_strLink = _strLink + "<a href='test3.aspx?page=1'>首页</a> ";
_strLink = _strLink + "<a href='test3.aspx?page=" + PrevPage().ToString() + "'>上一页</a> ";
_strLink = _strLink + "<a href='test3.aspx?page=" + NextPage().ToString() + "'>下一页</a> ";
_strLink = _strLink + "<a href='test3.aspx?page=" + PageCount().ToString() + "'>尾页</a> ";
_strLink = _strLink +"当前第 " + intPage.ToString() + " 页</div>";
return _strLink;
}
//计算前一页
public int PrevPage()
{
int _Page = intPage - 1;
if(_Page < 1)
{
_Page = 1;
}
return _Page;
}
//计算后一页
public int NextPage()
{
int _Page = intPage + 1;
if(_Page > PageCount())
{
_Page = PageCount();
}
return _Page;
}
//计算总记录数
public int RecordCount()
{
//所有文章总数
int _RecordCount = 0;
OleDbCommand cmdCommand = new OleDbCommand("select Count(id) from bbs_topic",conConnection);
conConnection.Open();
_RecordCount=(int)cmdCommand.ExecuteScalar();
conConnection.Close();
return _RecordCount;
}
//计算总页数
public int PageCount()
{
int _RecordCount = RecordCount();
int _PageCount;
if( _RecordCount % intPageSize != 0)
{
_PageCount = _RecordCount / intPageSize + 1;
}
else
{
_PageCount = _RecordCount / intPageSize;
}
return _PageCount;
}
//绑定显示数据
public void PageDataBind()
{
OleDbDataAdapter dadCommand;
if(intPage <= 1)
{
dadCommand = new OleDbDataAdapter("select top " + intPageSize + " id,title,addtime FROM bbs_topic order by id desc", conConnection);
}
else
{
//核心SQL语句,进行查询运算
dadCommand = new OleDbDataAdapter("Select Top " + intPageSize + " id,title,addtime from bbs_topic where (id not in(select top " + intPageSize * (intPage - 1) + " id from bbs_topic order by id desc)) order by id desc",conConnection);
}
DataSet dstDataSet = new DataSet();
dadCommand.Fill(dstDataSet,"News");
rptNews.DataSource = dstDataSet.Tables["News"].DefaultView;
rptNews.DataBind();
}
}
}
test3.aspx :
<%@ Page Inherits="test3.MyCodeBehind" src="cs/test3.cs" %>
<HTML>
<HEAD>
<title>test2</title>
</HEAD>
<body>
<asp:Repeater id="rptNews" Runat="server">
<ItemTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%" align="center"><%#DataBinder.Eval(Container.DataItem,"id")%></td>
<td width="70%" align="center"><%#DataBinder.Eval(Container.DataItem,"title")%></td>
<td width="20%" align="center"><%#DataBinder.Eval(Container.DataItem,"addtime")%></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:Literal id="ltrLink" runat="server"></asp:Literal>
</body>
</HTML>
[原创,首发于http://helloasp.net/bbs/topic.aspx?topicid=117,欢迎大家光临我的小站HelloASP.NET]