DataReader结合aspnetpager分页
以前一直使用DataTable与aspnetpager结合实现分页,今天测试一下DataReader
前台
前台
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CountRecordList.aspx.cs" Inherits="Office_CountRecordList" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataReader结合aspnetpager分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="1">
<tr class="meet_title bold">
<td>用户单位</td>
</tr>
<asp:Repeater ID="rptList" runat="server" OnItemCommand="rptList_ItemCommand">
<ItemTemplate>
<tr style='background-color:<%#(Container.ItemIndex%2==0)?"#fff":"#eee"%>' class="meet_tr" onmouseover="fOver(this);" onmouseout="fOut(this);">
<td style="height:30px;"><a href='CountRecord.aspx?rid=<%#Eval("PID") %>'><%#Eval("UnitName")%></a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td>
<webdiyer:aspnetpager ID="AspNetPager1" runat="server" PageSize="20" OnPageChanged="AspNetPager1_PageChanged"
AlwaysShow="True" CustomInfoHTML=" 当前第 <span style='color:red;'>%CurrentPageIndex%</span> 页,共 %PageCount%页"
FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页"
ShowCustomInfoSection="Right" Width="100%" BorderColor="#999999" CssClass="anpager">
</webdiyer:aspnetpager>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataReader结合aspnetpager分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="1">
<tr class="meet_title bold">
<td>用户单位</td>
</tr>
<asp:Repeater ID="rptList" runat="server" OnItemCommand="rptList_ItemCommand">
<ItemTemplate>
<tr style='background-color:<%#(Container.ItemIndex%2==0)?"#fff":"#eee"%>' class="meet_tr" onmouseover="fOver(this);" onmouseout="fOut(this);">
<td style="height:30px;"><a href='CountRecord.aspx?rid=<%#Eval("PID") %>'><%#Eval("UnitName")%></a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td>
<webdiyer:aspnetpager ID="AspNetPager1" runat="server" PageSize="20" OnPageChanged="AspNetPager1_PageChanged"
AlwaysShow="True" CustomInfoHTML=" 当前第 <span style='color:red;'>%CurrentPageIndex%</span> 页,共 %PageCount%页"
FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页"
ShowCustomInfoSection="Right" Width="100%" BorderColor="#999999" CssClass="anpager">
</webdiyer:aspnetpager>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台
代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AspNetPager1.RecordCount = Convert.ToInt32(getTotal());
Bind();
}
}
private void Bind()
{
IDataReader dr = getPagesData(Convert.ToInt32(AspNetPager1.PageSize), AspNetPager1.CurrentPageIndex - 1);
rptList.DataSource = dr;
rptList.DataBind();
dr.Close();
}
/// <summary>
/// 取得总数
/// </summary>
/// <returns></returns>
public string getTotal()
{
return LocalPub.ExecuteScalar("select count(*) as total from " + LocalPub.DBPrefix.Office + "t_CountRecord").ToString();
}
/// <summary>
/// 根据当前页码,每页条数,取得相应数据。
/// </summary>
/// <param name="pageNum">每页显示条数</param>
/// <param name="currentPage">当前页码</param>
/// <returns></returns>
public IDataReader getPagesData(int pageNum, int currentPage)
{
StringBuilder sb = new StringBuilder();
sb.Append("select top " + pageNum + " * from t_CountRecord ");
if (currentPage > 0)
sb.Append(" where PID not in (select top " + pageNum * currentPage + " PID from t_CountRecord order by PID desc)");
sb.Append(" order by PID desc");
return LocalPub.ExecuteReader(sb.ToString());
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
Bind();
}
{
if (!IsPostBack)
{
AspNetPager1.RecordCount = Convert.ToInt32(getTotal());
Bind();
}
}
private void Bind()
{
IDataReader dr = getPagesData(Convert.ToInt32(AspNetPager1.PageSize), AspNetPager1.CurrentPageIndex - 1);
rptList.DataSource = dr;
rptList.DataBind();
dr.Close();
}
/// <summary>
/// 取得总数
/// </summary>
/// <returns></returns>
public string getTotal()
{
return LocalPub.ExecuteScalar("select count(*) as total from " + LocalPub.DBPrefix.Office + "t_CountRecord").ToString();
}
/// <summary>
/// 根据当前页码,每页条数,取得相应数据。
/// </summary>
/// <param name="pageNum">每页显示条数</param>
/// <param name="currentPage">当前页码</param>
/// <returns></returns>
public IDataReader getPagesData(int pageNum, int currentPage)
{
StringBuilder sb = new StringBuilder();
sb.Append("select top " + pageNum + " * from t_CountRecord ");
if (currentPage > 0)
sb.Append(" where PID not in (select top " + pageNum * currentPage + " PID from t_CountRecord order by PID desc)");
sb.Append(" order by PID desc");
return LocalPub.ExecuteReader(sb.ToString());
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
Bind();
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。