• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

让DataList也能分页

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 37次 | 进入软件测试论坛讨论

领测软件测试网 众所周知,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" %>
<SCRIPT 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();

}
</SCRIPT>

<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>
</FORM>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网