Datalist分頁

Datalist分頁
衆所周知,ASP.Net中給我們提供了三個數據控件--DataGrid,Repeater,DataList。在這三個控件中,DataGrid控件的功能最強大,Repeater控件最忠實於模版原樣,DataList控件則兼而有之。
DataGrid控件太有名了,所以以前用的講的也很多,Repeater功能太少,沒有什麼好講的。這裏主要是講一講DataList控件。
DataList控件其實功能也很強大,他支持選擇、編輯,實現的方法也很簡單,不過最令人頭疼的就是它不像DataGrid控件一樣內置了分頁的功能,這麼好的一個控件竟然不能分頁!!!
確實是一個很讓人頭疼的事情。
不過,只是DataList沒有提供內置的分頁功能,但是並不表示,我們不能使用DataList控件來實現分頁,既然它不給我分頁功能,那隻好自己動手了。
下面是全部原代碼,其實用到的方法和PHP中的分頁差不多,只是這裏用的是DataAdapter與DataSet組合,而不是PHP中的SQL語句直接搞定。

<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<s cript language=C# Runat="Server">

/*
Create By 飛刀
http://www.aspcn.com
2001-7-25 01:44

Support .Net Framework Beta 2
DataList只有一個數據列,可以有多個按鈕列
*/


OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;


public void Page_Load(Object src,EventArgs e)
{
//設定PageSize
PageSize = 3;

//連接語句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..//images//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();

}
</s cript>

<FORM id=Form1 runat="server">
共有<asp:Label id=lblRecordCount runat="server" ForeColor="red"></asp:Label>條記錄
當前爲<asp:Label id=lblCurrentPage runat="server" ForeColor="red"></asp:Label>/<asp:Label id=lblPageCount runat="server" ForeColor="red"></asp:Label>頁
<asp:DataList id=score runat="server" EditItemStyle-BackColor="yellow" AlternatingItemStyle-BackColor="Gainsboro" HeaderStyle-BackColor="#aaaadd">
<ITEMTEMPLATE>
<%# DataBinder.Eval(Container.DataItem,"dcid") %><%# DataBinder.Eval(Container.DataItem,"title") %>
<asp:LinkButton id=Linkbutton1 runat="server" CommandName="edit" Text="傻瓜"></asp:LinkButton>
<asp:LinkButton id=btnSelect runat="server" CommandName="edit" Text="編輯"></asp:LinkButton>
</ITEMTEMPLATE>
</asp:DataList>
<asp:LinkButton id=lbnPrevPage runat="server" CommandName="prev" Text="上一頁" OnCommand="Page_onClick"></asp:LinkButton>
<asp:LinkButton id=lbnNextPage runat="server" CommandName="next" Text="下一頁" OnCommand="Page_onClick"></asp:LinkButton>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章