DataList控件也玩分页-转贴

DataList控件也玩分页
作者: 飞刀 人气:2568

作者: 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下测试通过)

1 @ Page Language="C#" 
1 @ Import Namespace="System.Data" 
1 @ Import Namespace="System.Data.OleDb" 
  1<script language="C#" runat="Server">   
  2/*   
  3Create By 飞刀   
  4http://www.aspcn.com   
  52001-7-25 01:44 
  6
  7Support .Net Framework Beta 2   
  8*/   
  9OleDbConnection MyConn;   
 10int PageSize,RecordCount,PageCount,CurrentPage;   
 11public void Page_Load(Object src,EventArgs e)   
 12{   
 13//设定PageSize   
 14PageSize = 10; 
 15
 16//连接语句   
 17string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\\DataBase\\\db1.mdb;";   
 18MyConn = new OleDbConnection(MyConnString);   
 19MyConn.Open(); 
 20
 21//第一次请求执行   
 22if(!Page.IsPostBack)   
 23{   
 24ListBind();   
 25CurrentPage = 0;   
 26ViewState["PageIndex"] = 0; 
 27
 28//计算总共有多少记录   
 29RecordCount = CalculateRecord();   
 30lblRecordCount.Text = RecordCount.ToString(); 
 31
 32//计算总共有多少页   
 33PageCount = RecordCount/PageSize;   
 34lblPageCount.Text = PageCount.ToString();   
 35ViewState["PageCount"] = PageCount;   
 36}   
 37}   
 38//计算总共有多少条记录   
 39public int CalculateRecord()   
 40{   
 41int intCount;   
 42string strCount = "select count(*) as co from Score";   
 43OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);   
 44OleDbDataReader dr = MyComm.ExecuteReader();   
 45if(dr.Read())   
 46{   
 47intCount = Int32.Parse(dr["co"].ToString());   
 48}   
 49else   
 50{   
 51intCount = 0;   
 52}   
 53dr.Close();   
 54return intCount;   
 55} 
 56
 57ICollection CreateSource()   
 58{ 
 59
 60int StartIndex; 
 61
 62//设定导入的起终地址   
 63StartIndex = CurrentPage*PageSize;   
 64string strSel = "select * from Score";   
 65DataSet ds = new DataSet(); 
 66
 67OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);   
 68MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); 
 69
 70return ds.Tables["Score"].DefaultView;   
 71}   
 72public void ListBind()   
 73{   
 74score.DataSource = CreateSource();   
 75score.DataBind(); 
 76
 77lbnNextPage.Enabled = true;   
 78lbnPrevPage.Enabled = true;   
 79if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;   
 80if(CurrentPage==0) lbnPrevPage.Enabled = false;   
 81lblCurrentPage.Text = (CurrentPage+1).ToString(); 
 82
 83} 
 84
 85public void Page_OnClick(Object sender,CommandEventArgs e)   
 86{   
 87CurrentPage = (int)ViewState["PageIndex"];   
 88PageCount = (int)ViewState["PageCount"]; 
 89
 90string cmd = e.CommandName;   
 91//判断cmd,以判定翻页方向   
 92switch(cmd)   
 93{   
 94case "next":   
 95if(CurrentPage<(PageCount-1)) CurrentPage++;   
 96break;   
 97case "prev":   
 98if(CurrentPage>0) CurrentPage--;   
 99break;   
100} 
101
102ViewState["PageIndex"] = CurrentPage; 
103
104ListBind(); 
105
106}   
107</script>
 1<html>
 2<head>
 3<title></title>
 4</head>
 5<body>
 6<form runat="server">   
 7共有<asp:label forecolor="red" id="lblRecordCount" runat="server"></asp:label>条记录    
 8当前为<asp:label forecolor="red" id="lblCurrentPage" runat="server"></asp:label>/<asp:label forecolor="red" id="lblPageCount" runat="server"></asp:label>页  
 9
10<asp:datalist alternatingitemstyle-backcolor="Gainsboro" edititemstyle-backcolor="yellow" headerstyle-backcolor="#aaaadd" id="score" runat="server">
11<itemtemplate>   
12姓名:```
13# DataBinder.Eval(Container.DataItem,"Name") 

<asp:linkbutton commandname="edit" id="btnSelect" runat="server" text="编辑"></asp:linkbutton> </asp:datalist> <asp:linkbutton commandname="prev" id="lbnPrevPage" oncommand="Page_OnClick" runat="server" text="上一页"></asp:linkbutton> <asp:linkbutton commandname="next" id="lbnNextPage" oncommand="Page_OnClick" runat="server" text="下一页"></asp:linkbutton>

```
Published At
Categories with Web编程
Tagged with
comments powered by Disqus