人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

DataList控件也玩分頁 (轉(zhuǎn)自aspcn.com)

DataList控件也玩分頁 (轉(zhuǎn)自aspcn.com)

更新時間:2022-06-06 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

眾所周知,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)問題去哪去問。問題太簡單了,還沒有人愿意回答。

多多思考,多多查資料,才是真正有收獲的。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 宜兰市| 临沂市| 崇明县| 和平县| 邢台县| 嘉定区| 齐齐哈尔市| 正阳县| 资源县| 新津县| 平度市| 宁阳县| 临泉县| 阳泉市| 聂拉木县| 鹤山市| 永年县| 色达县| 黄山市| 改则县| 杭锦后旗| 牙克石市| 武义县| 桂阳县| 清徐县| 崇礼县| 盐源县| 大兴区| 白山市| 京山县| 石渠县| 湖南省| 灵山县| 嘉祥县| 长白| 景洪市| 潞西市| 勃利县| 铁力市| 交口县| 连山|