分页控件 AspnetPager
2011-10-29 19:54 jiejiep 阅读(500) 评论(0) 编辑 收藏 举报--
在我们的项目中,通常会要对查询结果进行分页展示,以提高系统的响应速度,减轻服务器负荷。ASP.NET提供的 Gridview 控件虽然提供了分页,但是需要将查询的所有数据都提取绑定到Gridview后,才能显示分页工具栏,这样当数据量很大时,性能就会下降很多。
用户控件 AspnetPager 应运而生。它需要与分页存储过程联合使用。
--
AspNetPager 使用方法:
1. 将该用户控件放入工程,然后在aspx页面中添加引用。添加引用代码:<%@ Register Src="~/Parts/AspnetPager.ascx" TagName="AspNetPager" TagPrefix="pcc" %>
2. 只需要设置查询结果的总记录条数:TotalRecordsCount 属性值。另外还可以设置或修改 PageSize(每页显示记录条数)和 CurrentPageIndex(当前页号) 属性。
3. 在 OnPageIndex_Changed 事件中去重新绑定数据,并设置分页数据,主要是 TotalRecordsCount 属性。
--
分页存储过程如下:
IF OBJECT_ID('jjp_CategoryList_Get') IS NOT NULL
DROP PROCEDURE jjp_CategoryList_Get;
GO
CREATE PROCEDURE jjp_CategoryList_Get
(
@pageIndex int,
@PageSize int,
@Records int output
)
AS
begin
with tmpTable as
(
select categoryid, categoryname, description , row_number() over( order by categoryid) as rownum from dbo.categories
)
select * from tmpTable where rownum between (@pageIndex-1)*@PageSize+1 and @PageIndex*@PageSize;
select @Records=count(*) from dbo.Categories;
end
--
AspnetPager 控件ascx代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AspnetPager.ascx.cs" Inherits="Parts_AspnetPager"%>
<div>
<table style=" font-size:12px;">
<tr>
<td style="width: 32px">
<asp:LinkButton ID="LBtn_firstPage" runat="server" Text="首页|" OnClick="LBtn_firstPage_Click"></asp:LinkButton>
</td>
<td style="width: 32px">
<asp:LinkButton ID="LBtn_prePage" runat="server" Text="上页|" OnClick="LBtn_prePage_Click"></asp:LinkButton>
</td>
<td style="width: 32px">
<asp:TextBox ID="txt_PageIndex" runat="server" Width="62px" ToolTip="第几页" AutoPostBack="True"
BorderWidth="1px" BorderStyle="Solid" OnTextChanged="txt_PageIndex_TextChanged">1</asp:TextBox>
</td>
<td style="width: 32px">
<asp:LinkButton runat="server" Text="下页|" ID="LBtn_nextPage" OnClick="LBtn_nextPage_Click"></asp:LinkButton>
</td>
<td style="width: 32px">
<asp:LinkButton runat="server" Text="末页" ID="LBtn_lastPage" OnClick="LBtn_lastPage_Click"></asp:LinkButton>
</td>
<td style="width: 74px">
<font face="宋体">
<asp:Label ID="Label2" runat="server" Width="72px" Font-Size="9pt">| 每页行数 |</asp:Label></font>
</td>
<td style="width: 32px">
<asp:TextBox ID="txt_PageSize" runat="server" Width="56px" ToolTip="每页的行数" AutoPostBack="True"
BorderWidth="1px" BorderStyle="Solid" OnTextChanged="txt_PageSize_TextChanged">2</asp:TextBox>
</td>
<td style="width: 72px">
<font face="宋体">
<asp:Label ID="Label1" runat="server" Width="70px" Font-Size="9pt">| 总记录数|</asp:Label></font>
</td>
<td style="width: 74px">
<asp:Label ID="LableTotalCount" runat="server" Width="20px" Font-Size="9pt" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
</div>
--
AspnetPager.ascx.cs
--
/**************************************************************
* 作者:jjpeng
* 邮箱:ling121211@gmail.com
* 时间:2011-10-29 11:27:20
*-------------------------------------------------------------
* 功能说明:
* 1. 该控件用于与分页存储过程联合使用用于数据分页
* 2. 对外公开的属性有:
* --PageSize: 每页显示的记录条数(默认为2)
* --CurrentPageIndex: 当前的页号(如果是第一页则为1,默认为1)
* --TotalRecordsCount: 总的记录条数,需要在每次绑定完数据后设置值
* 3. 翻页时触发的事件
* --OnPageIndex_Changed: 只需要在此处重新执行绑定操作即可
*
*************************************************************/
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Parts_AspnetPager : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this._currentPageIndex = int.Parse(this.txt_PageIndex.Text.Trim( ));
this._pageSize = int.Parse(this.txt_PageSize.Text.Trim( ));
TotalPageCount = this._totalRecordsCount % this._pageSize == 0 ? this._totalRecordsCount / this._pageSize : this._totalRecordsCount / this._pageSize + 1;
}
}
public event Action<object, EventArgs> PageIndex_Chaged;
private int _currentPageIndex;
///<summary>
/// 当前页号
///</summary>
public int CurrentPageIndex
{
get
{
if (this._currentPageIndex == 0)
{
return int.Parse(txt_PageIndex.Text.Trim( ));
}
return this._currentPageIndex;
}
set
{
this._currentPageIndex = value;
this.txt_PageIndex.Text = value.ToString( );
}
}
private int _pageSize;
///<summary>
/// 每页显示记录条数
///</summary>
public int PageSize
{
get
{
if (this._pageSize == 0)
{
return int.Parse(txt_PageSize.Text.Trim( ));
}
return this._pageSize;
}
set
{
this._pageSize = value;
this.txt_PageSize.Text = value.ToString( );
}
}
private int _totalRecordsCount;
///<summary>
/// 总记录条数
///</summary>
public int TotalRecordsCount
{
get
{
if (this._totalRecordsCount == 0)
{
return int.Parse(this.LableTotalCount.Text);
}
return this._totalRecordsCount;
}
set
{
this._totalRecordsCount = value;
this.LableTotalCount.Text = value.ToString( );
}
}
///<summary>
/// 总页数
///</summary>
private static int TotalPageCount;
///<summary>
/// 第一页
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void LBtn_firstPage_Click(object sender, EventArgs e)
{
if (this._currentPageIndex != 1)
{
this._currentPageIndex = 1;
this.txt_PageIndex.Text = this._currentPageIndex.ToString( );
}
if (PageIndex_Chaged != null)
{
PageIndex_Chaged(sender, e);
}
}
///<summary>
/// 上一页
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void LBtn_prePage_Click(object sender, EventArgs e)
{
this._currentPageIndex = int.Parse(this.txt_PageIndex.Text.Trim( ));
if (this._currentPageIndex != 1 && this._currentPageIndex <= TotalPageCount)
{
this._currentPageIndex -= 1;
this.txt_PageIndex.Text = this._currentPageIndex.ToString( );
if (PageIndex_Chaged != null)
{
PageIndex_Chaged(sender, e);
}
}
}
///<summary>
/// 后一页
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void LBtn_nextPage_Click(object sender, EventArgs e)
{
//当前页号
this._currentPageIndex = int.Parse(this.txt_PageIndex.Text.Trim( ));
if (this._currentPageIndex < TotalPageCount)
{
this._currentPageIndex += 1;
this.txt_PageIndex.Text = this._currentPageIndex.ToString( );
if (PageIndex_Chaged != null)
{
PageIndex_Chaged(sender, e);
}
}
}
///<summary>
/// 最后一页
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void LBtn_lastPage_Click(object sender, EventArgs e)
{
this._currentPageIndex = int.Parse(this.txt_PageIndex.Text.Trim( ));
if (this._currentPageIndex != TotalPageCount)
{
this._currentPageIndex = TotalPageCount;
this.txt_PageIndex.Text = this._currentPageIndex.ToString( );
}
if (PageIndex_Chaged != null)
{
PageIndex_Chaged(sender, e);
}
}
//更改每页显示的记录条数
protected void txt_PageSize_TextChanged(object sender, EventArgs e)
{
this._currentPageIndex = int.Parse(txt_PageIndex.Text);
this._pageSize = int.Parse(this.txt_PageSize.Text.Trim( ));
//总记录数
this._totalRecordsCount = int.Parse(this.LableTotalCount.Text.Trim( ));
TotalPageCount = this._totalRecordsCount % this._pageSize == 0 ? this._totalRecordsCount / this._pageSize : this._totalRecordsCount / this._pageSize + 1;
if (this._currentPageIndex > TotalPageCount)
{
this._currentPageIndex = TotalPageCount;
this.txt_PageIndex.Text = this._currentPageIndex.ToString( );
}
if (PageIndex_Chaged != null)
{
PageIndex_Chaged(sender, e);
}
}
//页面跳转
protected void txt_PageIndex_TextChanged(object sender, EventArgs e)
{
//总记录数
this._totalRecordsCount = int.Parse(this.LableTotalCount.Text.Trim( ));
//当前页号
this._currentPageIndex = int.Parse(txt_PageIndex.Text);
//每页显示条数
this._pageSize = int.Parse(this.txt_PageSize.Text.Trim());
//如果当前页有效并且小于总页数
if (this._currentPageIndex>0 && this._currentPageIndex <= TotalPageCount )
{
if (PageIndex_Chaged != null)
{
PageIndex_Chaged(sender, e);
}
}
}
}
--
调用页面前台脚本和后台代码:
--
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test"%>
<%@ Register Src="~/Parts/AspnetPager.ascx" TagName="AspNetPager" TagPrefix="pcc"%>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="testDataGridview" runat="server"></asp:GridView>
<pcc:AspNetPager runat="server" ID="AspNetPager1" OnPageIndex_Chaged="AspNetPager1_PageIndexChanged"/>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetData( );
}
}
protected void AspNetPager1_PageIndexChanged(object sender, EventArgs e)
{
this.GetData( );
}
protected DataTable GetData( )
{
DataTable table = null;
using(SqlConnection conn = new SqlConnection("Data Source=.; Initial Catalog=Northwind; Integrated Security=True;"))
{
if(conn.State== ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand("jjp_CategoryList_Get", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pageIndex", this.AspNetPager1.CurrentPageIndex);
cmd.Parameters.AddWithValue("@PageSize", this.AspNetPager1.PageSize);
cmd.Parameters.Add("@Records", SqlDbType.Int);
cmd.Parameters["@Records"].Direction = ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet( );
da.Fill(ds);
this.testDataGridview.DataSource = ds.Tables[0];
this.testDataGridview.DataBind( );
this.AspNetPager1.TotalRecordsCount = int.Parse(cmd.Parameters["@Records"].Value.ToString( ));
}
return table;
}
}
路过的大虾,觉得实现不好的地方希望指出。感激不尽。