| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1084 人关注过本帖
标题:[求助]ASP.NET程序中用datalist实现分页
只看楼主 加入收藏
booo
Rank: 1
等 级:新手上路
帖 子:189
专家分:0
注 册:2006-6-24
收藏
 问题点数:0 回复次数:2 
[求助]ASP.NET程序中用datalist实现分页

ASP.NET程序中用datalist实现分页

搜索更多相关主题的帖子: NET datalist ASP 
2006-07-22 09:14
妙僧无花
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-4-8
收藏
得分:0 

网上有代码,你直接搜就能搜到.


<% @ 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.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)
  {
   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();
  
  }
  </script>
  <html>
  <head>
  <title></title>
  </head>
  <body>
  <form runat="server">
  共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录  
  当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页  
  
  <asp:DataList id="score" runat="server"
  HeaderStyle-BackColor="#aaaadd"
  AlternatingItemStyle-BackColor="Gainsboro"
  EditItemStyle-BackColor="yellow"
  >
   <ItemTemplate>
   姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
   <asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
   </ItemTemplate>
  </asp:DataList>
  <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
  <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
  
  </form>
  </body>
  </html>

2006-07-23 19:34
飞鱼
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-2-27
收藏
得分:0 
看起来挺复杂的,不知道能不能简单一些啊!!
2006-07-26 16:47
快速回复:[求助]ASP.NET程序中用datalist实现分页
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015918 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved