眾所周知,ASP.Net中給我們提供了三個數(shù)據(jù)控件--DataGrid,Repeater,DataList。在這三個控件中,DataGrid控件的功能最強大,Repeater控件最忠實于模版原樣,DataList控件則兼而有之。
DataGrid控件太有名了,所以以前用的講的也很多,Repeater功能太少,沒有什么好講的。這里主要是講一講DataList控件。
DataList控件其實功能也很強大,他支持選擇、編輯,實現(xiàn)的方法也很簡單,不過最令人頭疼的就是它不像DataGrid控件一樣內(nèi)置了分頁的功能,這么好的一個控件竟然不能分頁!!!
確實是一個很讓人頭疼的事情。
不過,只是DataList沒有提供內(nèi)置的分頁功能,但是并不表示,我們不能使用DataList控件來實現(xiàn)分頁,既然它不給我分頁功能,那只好自己動手了。
下面是全部原代碼,其實用到的方法和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) { //設(shè)定PageSize PageSize = 10;
//連接語句 string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;"; MyConn = new OleDbConnection(MyConnString); MyConn.Open();
//第一次請求執(zhí)行 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;
//設(shè)定導(dǎo)入的起終地址 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" />條記錄 當(dāng)前為<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>
運行結(jié)果如上圖:)
大家在寫程序時,最重要的是自己去動腦去想,決對不是一出現(xiàn)問題去哪去問。問題太簡單了,還沒有人愿意回答。
多多思考,多多查資料,才是真正有收獲的。
|