使用實現起來雖然比較方便,但是效率不高,每次都需要讀取所有頁(整個記錄集),而加載的只是其中一頁,造成了資源的浪費,記錄多又會使效率變得很低。下面通過DataGrid的自定義分頁功能來減少資源使用和提高效率。
實現的關鍵是設置AllowCustomPaging屬性位True,並把VirtualItemCount屬性設置位總的記錄數,給分頁提供依據,前台的主要代碼如下:
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"
border="1">
<TR>
<TD>
<asp:datagrid id="DataGrid1" runat="server" Width="100%" AllowPaging="True" AllowCustomPaging="True">
<PagerStyle Font-Size="9pt" Mode="NumericPages"></PagerStyle>
</asp:datagrid></TD>
</TR>
</TABLE>
</form>
這裡使用的數據源還是假設為Northwind的Customers表。
下面是訪問單頁的存儲過程,實現方式很多,不過這個是最普通的,
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
獲取記錄數和頁數都采用存儲過程的輸出參數。
獲取數據源,這裡返回一個DataSet。
先定義了連個數據成員,
private int pageCount;//頁數
private int recordCount;//記錄數
//獲取單頁數據
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;
}
//綁定數據到DataGrid,同時刷新數據總記錄數
private void DataGridDataBind()
{
DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);
this.DataGrid1.VirtualItemCount = RecordCount;
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
}
下面是分頁的幾個變量屬性
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;}
}
注冊DataGrid分頁事件
//分頁事件處理
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid dg = (DataGrid)source;
dg.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
}
最好判斷當前頁面是否是第一次加載,防止重復加載兩次數據,
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)