ΪDataGrid×Ô¶¨Òå·ÖÒ³Ìí¼Ó×Ô¶¨Òåµ¼º½ºÍ·ÖÒ³ÐÅÏ¢
ΪDataGrid×Ô¶¨Òå·ÖÒ³Ìí¼Ó×Ô¶¨Òåµ¼º½ºÍ·ÖÒ³ÐÅÏ¢
Ö£ ×ô
ÔÚÉÏһƪÎÄÕÂÖÐÎÒ½²µ½Á˶ÔDataGridʵÐÐ×Ô¶¨Òå·ÖÒ³£¬Õâ¿ÉÒÔ±ÜÃâΪÁËÏÔʾһҳÊý¾Ý¶ø»ñÈ¡Õû¸öÊý¾Ý¼Ç¼¼¯£¬´Ó¶øÌá¸ß·ÖҳЧÂÊ£¬²»¹ýʹÓõĵ¼º½»¹ÊÇDataGrid×Ô´øµÄÊý×ÖÁ¬½Ó»ò¼òµ¥µÄÉÏÒ»Ò³£¬ÏÂÒ»Ò³£¬¶øÇÒ¿´²»µ½×ÜÒ³Êý¡¢×ܼǼÊýÖ®ÀàµÄÐÅÏ¢¡£ÏÂÃæ¾ÍΪËûÔö¼ÓÎÒÃÇËùÐèÒªµÄ²¿·Ö¡£
ÏÈÀ´¿´¿´Ð޸ĺóµÄ·ÖÒ³ÏÔʾ£¬½ØÍ¼ÈçÏ£º

£¨Í¼Ò»£©
ʹÓõÄÊý¾ÝԴͬÉÏһƪÎÄÕ£¨Asp.netÖÐDataGrid¿Ø¼þµÄ×Ô¶¨Òå·ÖÒ³£©Ïàͬ£¬¶¼ÊÇ·ÃÎÊNorthwind¿â£¬ÎªÁ˶ÀÁ¢¿ªÀ´ÕâÀﻹÊǰѴ洢¹ý³ÌÁÐÁËһϣ¬
CREATE PROCEDURE [GetCustomersDataPage]
@PageIndex INT,
@PageSize INT,
@RecordCount INT OUT,
@PageCount INT OUT
AS
SELECT @RecordCount = COUNT(*) FROM Customers
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)
DECLARE @SQLSTR NVARCHAR(1000)
IF @PageIndex = 0 OR @PageCount <= 1
SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+
' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID DESC
ELSE IF @PageIndex = @PageCount - 1
SET @SQLSTR =N' SELECT * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+
' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'
ELSE
SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+' * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+
' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'
EXEC (@SQLSTR)
GO
ÏÂÃæ¾Í¾Í°Ñ´úÂëÌùÁËһϣ¬
AspxÎļþ´úÂëÈçÏ£º
<%@ Page language="c#" Codebehind="DataGridCustomPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridCustomPaging" %>
<!DOCTYPE HTML PUBLIC "-//W
<HTML>
<HEAD>
<title>DataGridPaging</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="FONT-SIZE:
border="1">
<TR>
<TD><asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" AllowCustomPaging="True" Width="100%">
<FooterStyle Font-Size="
<HeaderStyle Font-Size="
<PagerStyle Visible="False" Font-Size="
</asp:datagrid></TD>
</TR>
<TR>
<TD>
<TABLE id="Table2" style="FONT-SIZE:
align="center" border="1">
<TR>
<TD style="WIDTH: 150px"><asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">Ê×Ò³</asp:linkbutton>
<asp:linkbutton id="LBtnPrev" runat="server" CommandName="Prev">ÉÏÒ»Ò³</asp:linkbutton>
<asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">ÏÂÒ»Ò³</asp:linkbutton>
<asp:linkbutton id="LBtnLast" runat="server" CommandName="Last">βҳ</asp:linkbutton></TD>
<TD>µÚ<asp:literal id="LtlPageIndex" runat="server"></asp:literal>Ò³ ¹²<asp:literal id="LtlPageCount" runat="server"></asp:literal>Ò³
ÿҳ<asp:Literal id="LtlPageSize" runat="server"></asp:Literal>Ìõ ¹²<asp:Literal id="LtlRecordCount" runat="server"></asp:Literal>Ìõ
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
Aspx.csÎļþ´úÂëÈçÏ£º
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace ZZ.AspnetPaging
{
public class DataGridCustomPaging : System.Web.UI.Page
{
private int pageCount;
private int recordCount;
protected System.Web.UI.WebControls.LinkButton LBtnFirst;
protected System.Web.UI.WebControls.LinkButton LBtnPrev;
protected System.Web.UI.WebControls.LinkButton LBtnNext;
protected System.Web.UI.WebControls.LinkButton LBtnLast;
protected System.Web.UI.WebControls.Literal LtlPageIndex;
protected System.Web.UI.WebControls.Literal LtlPageCount;
protected System.Web.UI.WebControls.Literal LtlPageSize;
protected System.Web.UI.WebControls.Literal LtlRecordCount;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
DataGridDataBind();
}
}
//°ó¶¨Êý¾Ý
private void DataGridDataBind()
{
DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);
this.DataGrid1.VirtualItemCount = RecordCount;
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
SetPagingState();
}
#region Web ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.LBtnFirst.Click += new System.EventHandler(this.LBtnNavigation_Click);
this.LBtnPrev.Click += new System.EventHandler(this.LBtnNavigation_Click);
this.LBtnNext.Click += new System.EventHandler(this.LBtnNavigation_Click);
this.LBtnLast.Click += new System.EventHandler(this.LBtnNavigation_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)
{
string connString = ConfigurationSettings.AppSettings["ConnString"];
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn);
comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));
comm.Parameters[0].Value = pageIndex;
comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));
comm.Parameters[1].Value = pageSize;
comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));
comm.Parameters[2].Direction = ParameterDirection.Output;
comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));
comm.Parameters[3].Direction = ParameterDirection.Output;
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
recordCount = (int)comm.Parameters[2].Value;
pageCount = (int)comm.Parameters[3].Value;
return ds;
}
private void LBtnNavigation_Click(object sender, System.EventArgs e)
{
LinkButton btn = (LinkButton)sender;
switch(btn.CommandName)
{
case "First":
PageIndex = 0;
break;
case "Prev"://if( PageIndex > 0 )
PageIndex = PageIndex - 1;
break;
case "Next"://if( PageIndex < PageCount -1)
PageIndex = PageIndex + 1;
break;
case "Last":
PageIndex = PageCount - 1;
break;
}
DataGridDataBind();
}
/// <summary>
/// ¿ØÖƵ¼º½°´Å¥»òÊý×ÖµÄ״̬
/// </summary>
public void SetPagingState()
{
if( PageCount <= 1 )//( RecordCount <= PageSize )//СÓÚµÈÓÚÒ»Ò³
{
this.LBtnFirst.Enabled = false;
this.LBtnPrev.Enabled = false;
this.LBtnNext.Enabled = false;
this.LBtnLast.Enabled = false;
}
else //ÓжàÒ³
{
if( PageIndex == 0 )//µ±Ç°ÎªµÚÒ»Ò³
{
this.LBtnFirst.Enabled = false;
this.LBtnPrev.Enabled = false;
this.LBtnNext.Enabled = true;
this.LBtnLast.Enabled = true;
}
else if( PageIndex == PageCount - 1 )//µ±Ç°Îª×îºóÒ³
{
this.LBtnFirst.Enabled = true;
this.LBtnPrev.Enabled = true;
this.LBtnNext.Enabled = false;
this.LBtnLast.Enabled = false;
}
else //ÖмäÒ³
{
this.LBtnFirst.Enabled = true;
this.LBtnPrev.Enabled = true;
this.LBtnNext.Enabled = true;
this.LBtnLast.Enabled = true;
}
}
this.LtlPageSize.Text = PageSize.ToString();
this.LtlRecordCount.Text = RecordCount.ToString();
if(RecordCount == 0)
{
this.LtlPageCount.Text = "0";
this.LtlPageIndex.Text = "0";
}
else
{
this.LtlPageCount.Text = PageCount.ToString();
this.LtlPageIndex.Text = (PageIndex + 1).ToString();
}
}
public int PageCount
{
get
{
return this.DataGrid1.PageCount;
}
}
public int PageSize
{
get
{
return this.DataGrid1.PageSize;
}
}
public int PageIndex
{
get
{
return this.DataGrid1.CurrentPageIndex;
}
set
{
this.DataGrid1.CurrentPageIndex = value;
}
}
public int RecordCount
{
get
{
return recordCount;
}
}
}
}
ÉÏÃæµÄ´úÂë±È½Ï¼òµ¥£¬Ò²¾Í²»Ó÷ÖÎöÁË£¬Èç¹ûÓÐʲôºÃµÄ½¨Òé»òÓÐÎÊÌâ¿ÉÒÔÔÚBlogÉÏÁôÑÔ£¬ºÜ¸ßÐËͬ´ó¼Ò½»Á÷¡£



ÎÄÕÂÆÀÂÛ
¹²ÓÐ Î»ÍøÓÑ·¢±íÁËÆÀÂÛ ²é¿´ÍêÕûÄÚÈÝ