一分心灵的宁静

在滚滚红尘,繁杂人世里,能够保持一分心灵的宁静,随时回到自己的内心深处,细细品味生命的奥妙,无疑是一种修身养性的人生境界

导航

DataList分页

Posted on 2006-02-13 16:17  有缘无份  阅读(183)  评论(0编辑  收藏  举报

 

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