当前位置: 首页 > 图文教程 > 网络编程 > ASP > DataList控件也玩分页

ASP
关于网站文件自动备份程序的一点思考
DBTree 1.3.2
抽取10万条数据,想起GetRows()
一份ASP内存的释放的实验报告
[整理版]ASP常用内置函数
Jmail组件发送邮件之绝对能用的函数
虚拟主机重启代码
Access 开发人员常犯错误大全
用Asp如何实现防止网页频繁刷新?
ASP 中使用 HTTP 协议发送参数详解
为什么 Windows2003 的 IIS6.0 不能上传超过 200K 的文件?
ASP类的写法
实例学习如何在ASP中调用DLL
被动式统计网站在线人数
[原创]完美解决ASP 不能更新。数据库或对象为只读。
5天学会asp
Wrance的图片系统目录直读版1.0
信息发布中的判断过期和有效期的东西
小偷程序2
一个ASP中的数组

ASP 中的 DataList控件也玩分页


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 171 ::
收藏到网摘: n/a

       作者:http://www.aspcn.com 飞刀
  
  众所周知,ASP.Net中给我们提供了三个数据控件--DataGrid,Repeater,DataList。在这三个控件中,DataGrid控件的功能最强大,Repeater控件最忠实于模版原样,DataList控件则兼而有之。
  
  DataGrid控件太有名了,所以以前用的讲的也很多,Repeater功能太少,没有什么好讲的。这里主要是讲一讲DataList控件。
  
  DataList控件其实功能也很强大,他支持选择、编辑,实现的方法也很简单,不过最令人头疼的就是它不像DataGrid控件一样内置了分页的功能,这么好的一个控件竟然不能分页!!!
  
  确实是一个很让人头疼的事情。
  
  不过,只是DataList没有提供内置的分页功能,但是并不表示,我们不能使用DataList控件来实现分页,既然它不给我分页功能,那只好自己动手了。
  
  下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。
  
  (本程序在.Net Framework Beta 2下测试通过)
  
  
  <% @ Page Language="C#" %>
  <% @ Import Namespace="System.Data" %>
  <% @ Import Namespace="System.Data.OleDb" %>
  <Script Language="C#" Runat="Server">
  /*
   Create By 飞刀
   http://www.aspcn.com
   2001-7-25 01:44
  
   Support .Net Framework Beta 2
  */
  OleDbConnection MyConn;
  int PageSize,RecordCount,PageCount,CurrentPage;
  public void Page_Load(Object src,EventArgs e)
  {
   //设定PageSize
   PageSize = 10;
  
   //连接语句
   string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
   MyConn = new OleDbConnection(MyConnString);
   MyConn.Open();
  
   //第一次请求执行
   if(!Page.IsPostBack)
   {
   ListBind();
   CurrentPage = 0;
   ViewState["PageIndex"] = 0;
  
   //计算总共有多少记录
   RecordCount = CalculateRecord();
   lblRecordCount.Text = RecordCount.ToString();
  
   //计算总共有多少页
   PageCount = RecordCount/PageSize;
   lblPageCount.Text = PageCount.ToString();
   ViewState["PageCount"] = PageCount;
   }
  }
  //计算总共有多少条记录
  public int CalculateRecord()
  {
   int intCount;
   string strCount = "select count(*) as co from Score";
   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 Score";
   DataSet ds = new DataSet();
  
   OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
   MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
  
   return ds.Tables["Score"].DefaultView;
  }
  public void ListBind()
  {
   score.DataSource = CreateSource();
   score.DataBind();
  
   lbnNextPage.Enabled = true;
   lbnPrevPage.E