ページングコントロールAspnetPager

45368 ワード

--
デルのプロジェクトでは、通常、クエリーの結果をページングして、システムの応答速度を向上させ、サーバ負荷を軽減します.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
* :[email protected]
* :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;
}
}

通りすがりのエビは、実現が悪いと感じたところを指摘してほしい.感謝にたえない.