众所周知,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>