DataReader也玩超酷数据显示分页

    用DataReader实现分页,让我头疼了好多天。为什么我非的用DataReader控件实现分页呢?大家看看(http://www.wrclub.net/mb/)这个页面,也许有点明白了吧!对数据的横竖排列我们可以随意控制!

    ASP.net中,最容易完成分页操作的是DataGrid,其次是DataList控件;但它们对数据的显示却做了限制,使得数据显示变得很难操作。DataReader模拟快速、仅向前的只读游标的操作,完成分页根本不可能,针对DataReader实现分页也只能从sql语句分析,但它对数据显示完全自由,我们可以根据自己的意愿来定义数据的显示方式!

代码如下:

<%@ Page Language="C#" Debug="true" %>
<%@OutputCache Duration="3" VaryByParam="none"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<Script Language="C#" runat="server">
//以下源码由网人俱乐部提供,作者:福星,网址:http://www.wrclub.net/
//转摘请注明出处和作者
//定义每页显示记录数
int PageSize=9;
SqlConnection conn;
void Page_Load(Object src,EventArgs e)
{
int RecordCount,PageCount,CurrentPage,startID,endID;
//建立与数据库的连接
conn =new SqlConnection(ConfigurationSettings.AppSettings["strconn"]);
conn.Open();
//第一次请求执行
if(!Page.IsPostBack)
{
  //计算总共有多少记录
  RecordCount = CalculateRecord();
  //计算总共有多少页
  //取整
  PageCount = RecordCount/PageSize;
  if (RecordCount%PageSize > 0)
  PageCount = PageCount + 1;
  lblPageCount.Text = PageCount.ToString();
  lblRecordCount.Text = RecordCount.ToString();
  CurrentPage = 0;
}
//定义一个临时变量
string startIDt,endIDt;
//设置下一页开始ID
startID=0;
//设置上一页结束ID
endID=0;
startIDt=Request.Params["sid"];
endIDt=Request.Params["eid"];
//获得下一页开始ID
if(startIDt!=null)
startID=Convert.ToInt32(startIDt);
//获得上一页结束ID
if(endIDt!=null)
endID=Convert.ToInt32(endIDt);
//sql语句开始部分
string ConnStr="select top "+PageSize+" * from article where classid=1 ";
//如果有下一页开始ID的值,说明点击了“下一页”,条件语句查询的ID小于开始ID(这里排序为ID倒序)
if(startID>0)
ConnStr+=" and id<"+startID;
//如果有上一页结束ID的值,说明点击了“上一页”,条件语句查询的ID大于结束ID(这里排序为ID倒序)
if(endID>0)
ConnStr+=" and id>"+endID;
//ID倒序
ConnStr+=" order by id desc";
//DataReader执行以上sql语句
SqlCommand cmd=new SqlCommand(ConnStr,conn);
SqlDataReader myReader = cmd.ExecuteReader();
string html="<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%' height='139' bgcolor='#ffffff'><tr>";
int i=0;
while(myReader.Read())
{
  //获得上一页结束ID值
  if(i==0)endID=Convert.ToInt32(myReader[0]);
  html+="<td align=center height=180 ><table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='145'><tr><td align=center><img src='"+myReader[2].ToString().Trim()+"/"+myReader[3].ToString().Trim()+"' height=143 width=140 border=0 alt='"+myReader[1].ToString().Trim()+"'></td></tr><tr><td width='100%' background='images/mbbg.gif'><table border='0'cellpadding='0'cellspacing='0'width='100%'><tr><td width='50%'><a href='"+myReader[2].ToString().Trim()+"/' target='_blank'><img src='images/view.gif' border=0></a></td><td width='50%' align=right><a href='mbdown.aspx?id="+myReader[0]+"' target=_blank><img src='images/down.gif' border=0></a></td></tr></table></td></tr></table></td>";
  i++;
  if(i%3==0)
    html+="</tr><tr>";
  //获得下一页开始ID值
  if(i==PageSize)startID=Convert.ToInt32(myReader[0]);
}
myReader.Close();
html+="</tr></table>";
mblist.InnerHtml=html;

//获得页的值
if(Request.Params["page"]!=null)
  CurrentPage = Convert.ToInt32(Request.Params["page"]);
if(PageCount==0)
{
  lblCurrentPage.Text = "0";
  pagelist.InnerHtml="上一页  下一页";
}
else
{
  lblCurrentPage.Text = CurrentPage.ToString();
  pagelist.InnerHtml="<a href='http://www.wrclub.net/mblist.aspx?id=22&classname=模板中心&page="+(CurrentPage-1)+"&eID="+endID+"'>上一页</a> <a href='http://www.wrclub.net/mblist.aspx?id=22&classname=模板中心&page="+(CurrentPage+1)+"&sID="+startID+"'>下一页</a>";
  if(CurrentPage==PageCount)
   pagelist.InnerHtml="<a href='http://www.wrclub.net/mblist.aspx?id=22&classname=模板中心&page="+(CurrentPage-1)+"&eID="+endID+"'>上一页</a> 下一页";
  if(CurrentPage==0)
  {
   pagelist.InnerHtml="上一页 <a href='http://www.wrclub.net/mblist.aspx?id=22&classname=模板中心&page="+(CurrentPage+2)+"&sID="+startID+"'>下一页</a>";  
   lblCurrentPage.Text = (CurrentPage+1).ToString();
  }
}
}
//获得记录总数
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) from article where classid=1";
SqlCommand MyComm = new SqlCommand(strCount,conn);
SqlDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
  intCount = Int32.Parse(dr[0].ToString());
}
else
{
  intCount = 0;
}
dr.Close();
return intCount;
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>模板中心:网人俱乐部</title>
<meta name="Keywords" CONTENT="学习,免费资源,asp,jsp,flash,photoshop,社区,论坛,同学录,电子图书,网站建设,宣传,推广,聊天,数据库,经验心得,游戏,game,电子超市,网人交流论坛,插件,下载">
<meta name="description" CONTENT="提供网站建设、开发方面的学习资料、经验心得,同时提供免费的电子书籍、网站免费资源、制作工具、插件、滤镜,网站还有网上电子超市、论坛、网人交流论坛">
<link rel="stylesheet" type="text/css" href="style.css"><link rel="Shortcut Icon" href="favicon.ico">
</head>
<body topmargin="0" leftmargin="0"><form runat=server>
<!-- #Include File="top.inc" -->
<table width="778" border="0" cellpadding="0" cellspacing="0" bgcolor="#D8D8D8">
  <tr>
   <td width="562" height="400" align="center" valign="top"><br>
<!-- 数据显示位置  -->
<span id="mblist" runat="server"></span>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
  <tr>
    <td align=center>共有<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=center>
<!-- 分页显示位置  -->
      <span id="pagelist" runat="server"></span>
    </td>
  </tr>
</table>
</td></tr></table>
</form>
</body></html>

注明:下一页开始ID值为最接近下一页开始ID值,上一页结束ID为最接近上一页结束ID。我这个写的相对实现了数据的自定义显示,对于分页功能还不是很强大,大家可以在此基础上增加功能。

源出处:http://www.wrclub.net/study/listarticle.aspx?id=1157

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值