实现DataList控件的分页
2024-07-21 02:24:15
供稿:网友
datalist控件功能很强大,他支持选择、编辑,实现的方法也很简单,不过最令人头疼的就是它不像datagrid控件一样内置了分页的功能,这么好的一个控件竟然不能分页!!!确实是一个很让人头疼的事情。
不过,只是datalist没有提供内置的分页功能,但是并不表示,我们不能使用datalist控件来实现分页,既然它不给我分页功能,那只好自己动手了。
下面是全部原代码,其实用到的方法和php中的分页差不多,只是这里用的是dataadapter与dataset组合,而不是php中的sql语句直接搞定。
default.aspx文件:
<%@ page language="c#" codebehind="default.aspx.cs" autoeventwireup="false" inherits="guestbook._default" %>
<html>
<head>
<title>中国bs网留言簿</title>
<html>
<head>
<title>datalist分页</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
</head>
<body ms_positioning="gridlayout">
<form method="post" action="default.aspx" runat="server" id="form1">
<table width="100%" border="0" cellpadding="2" cellspacing="0" align="center">
<tr>
<td align="middle">
<asp:datalist id="myguest" repeatcolumns="1" runat="server">
<itemtemplate>
<table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center" width="610">
<tr>
<td>
<table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center" width="610">
<tr>
<td>
<table width="100%" border="0" cellpadding="6" cellspacing="0" class="gueststylebg" align="center">
<tr>
<td width="112" align="center" height="153" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%" class="gueststylebg">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" align="center" height="25">
<tr>
<td class="filter">
<div align="center"><font ><%# databinder.eval(container.dataitem,"name") %></font></div>
</td>
</tr>
</table>
<div align="center"><font ><img src="<%# databinder.eval(container.dataitem,"face") %>" width="80" height="90" align="middle"></font><br>
<br>
★
<%# databinder.eval(container.dataitem,"sex") %>
★</div>
</td>
</tr>
</table>
</td>
<td align="center" height="153" width="2">
<table class="gueststyleline" width="1" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="1"></td>
</tr>
</table>
</td>
<td height="33" valign="top" width="463">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" height="20">
<tr>
<td height="14" width="310"><img src="http://www.163design.net/n/i/images/icon/subject.gif" width="15" height="15" alt="主题">:<%# databinder.eval(container.dataitem,"caption") %><br>
</td>
<td width="151" height="14"><font > <img src="http://www.163design.net/n/i/images/icon/posttime.gif" width="16" height="15" alt="发表时间[北京时间]">:<%# databinder.eval(container.dataitem,"postdate") %></font></td>
</tr>
</table>
<hr class="gueststylehr" size="0.1" width="100%">
<table width="100%" border="0" cellpadding="5" cellspacing="0" class="gueststylebg" height="45%" >
<tr>
<td valign="top" width="28" align="center"><img src="http://www.163design.net/n/i/images/emot/1.gif" width="16" height="16">
</td>
<td valign="top" width="94%" >
<%# databinder.eval(container.dataitem,"content") %>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr>
<td valign="top">
<hr class="gueststylehr" size="0.1" width="100%">
<a href="<%# databinder.eval(container.dataitem,"homepage") %>" target="_blank"><img src="http://www.163design.net/n/i/images/icon/home.gif" width="44" height="16" border="0"></a>
<a href="http://www.chinagz.net/ip/ipsearch.cgi?ip=<%# databinder.eval(container.dataitem,"ip") %>" target="_blank"><img src="http://www.163design.net/n/i/images/icon/ip.gif" align="absmiddle" height="16" border="0"></a>
<a href="http://search.tencent.com/cgi-bin/friend/user_show_info?ln=<%# databinder.eval(container.dataitem,"qq") %>" target="_blank">
<img src="http://www.163design.net/n/i/images/icon/qq.gif" border="0" width="35" height="16"></a>
<a href="mailto:<%# databinder.eval(container.dataitem,"email") %>"><img src="http://www.163design.net/n/i/images/icon/email.gif" width="42" height="16" border="0" alt=""></a>
<a href="javascript:;" onclick="mm_showhidelayers("admin","","hide","postcontent","","show")">
<img src="http://www.163design.net/n/i/images/icon/postguest.gif" width="45" height="16" border="0"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist>
<br>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" height="9">
<tbody>
<tr>
<td align="middle">
<table class="gueststyleline" width="610" border="0" cellpadding="1" cellspacing="0" height="18">
<tr>
<td height="24">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" height="18">
<tr>
<td width="35%" align="middle" height="36">
共有<asp:label id="lblrecordcount" forecolor="red" runat="server" />条记录 当前为<asp:label id="lblcurrentpage" forecolor="red" runat="server" />/<asp:label id="lblpagecount" forecolor="red" runat="server" />页
</td>
<td align="middle" height="36">
<asp:linkbutton id="lbnprevpage" text="上一页" commandname="prev" runat="server" />
<asp:linkbutton id="lbnnextpage" text="下一页" commandname="next" runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
注意:必须把两个linkbutton控件的command事件改为page_onclick:
codebehind文件:
private void page_load(object sender, system.eventargs e)
{
pagesize=1;
conn= new sqlconnection(application["guest_conn"].tostring());
conn.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 guest";
sqlcommand mycomm = new sqlcommand (strcount,conn);
sqldatareader 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 guest order by postdate desc";
dataset ds = new dataset();
sqldataadapter myadapter = new sqldataadapter(strsel,conn);
myadapter.fill(ds,startindex,pagesize,"guest");
return ds.tables["guest"].defaultview;
}
public void listbind()
{
myguest.datasource = createsource();
myguest.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();
}