浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

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='http://cfan.net.cn/info/images/view.gif' border=0></a></td><td width='50%' align=right><a href='mbdown.aspx?id="+myReader[0]+"' target=_blank><img src='http://cfan.net.cn/info/images/down.gif' border=0></a></td></tr></td></tr></td>";
i++;
if(i%3==0)
html+="</tr><tr>";
//获得下一页开始ID值
if(i==PageSize)startID=Convert.ToInt32(myReader[0]);
}
myReader.Close();
html+="</tr>";
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>

</td></tr>
</form>
</body></html>

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

相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...