ASP.NET分页方法(C#)

分页是个大问题,,终于走出了第一步了我,,庆祝庆祝!

文件代码如下:

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() + " 记录&nbsp;&nbsp;&nbsp;&nbsp;";
           _strLink = _strLink + "<a href='test3.aspx?page=1'>首页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
           _strLink = _strLink + "<a href='test3.aspx?page=" + PrevPage().ToString() + "'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
           _strLink = _strLink + "<a href='test3.aspx?page=" + NextPage().ToString() + "'>下一页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
           _strLink = _strLink + "<a href='test3.aspx?page=" + PageCount().ToString() + "'>尾页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
           _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]

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值