** 为 ** ** DataGrid ** ** 自定义分页添加自定义导航和分页信息 ** ** **
郑 佐 2004-10-29
在上一篇文章中我讲到了对 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 文件代码如下:
1@ Page language="c#" Codebehind="DataGridCustomPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridCustomPaging"
1<html>
2<head>
3<title>DataGridPaging</title>
4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
5<meta content="C#" name="CODE_LANGUAGE"/>
6<meta content="JavaScript" name="vs_defaultClientScript"/>
7<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
8</head>
9<body>
10<form id="Form1" method="post" runat="server">
11<table align="center" border="1" cellpadding="1" cellspacing="1" id="Table1" style="FONT-SIZE: 9pt " width="450">
12<tr>
13<td><asp:datagrid allowcustompaging="True" allowpaging="True" id="DataGrid1" runat="server" width="100%">
14<footerstyle font-size=" 9pt "></footerstyle>
15<headerstyle font-size=" 9pt "></headerstyle>
16<pagerstyle font-size=" 9pt " mode="NumericPages" visible="False"></pagerstyle>
17</asp:datagrid></td>
18</tr>
19<tr>
20<td>
21<table align="center" border="1" cellpadding="1" cellspacing="1" id="Table2" style="FONT-SIZE: 9pt " width="100%">
22<tr>
23<td style="WIDTH: 150px"><asp:linkbutton commandname="First" id="LBtnFirst" runat="server"> 首页 </asp:linkbutton>
24
25<asp:linkbutton commandname="Prev" id="LBtnPrev" runat="server"> 上一页 </asp:linkbutton>
26
27<asp:linkbutton commandname="Next" id="LBtnNext" runat="server"> 下一页 </asp:linkbutton>
28
29<asp:linkbutton commandname="Last" id="LBtnLast" runat="server"> 尾页 </asp:linkbutton></td>
30<td> 第 <asp:literal id="LtlPageIndex" runat="server"></asp:literal> 页 共 <asp:literal id="LtlPageCount" runat="server"></asp:literal> 页
31
32每页 <asp:literal id="LtlPageSize" runat="server"></asp:literal> 条 共 <asp:literal id="LtlRecordCount" runat="server"></asp:literal> 条
33
34</td>
35</tr>
36</table>
37</td>
38</tr>
39</table>
40</form>
41</body>
42</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;
&