|
在一般情況下,DataGrid控件每次實現(xiàn)翻頁操作時,都會將數(shù)據(jù)源中的數(shù)據(jù)重新調(diào)用一次,當(dāng)數(shù)據(jù)中
數(shù)據(jù)很多時,這樣做就會很浪費(fèi)系統(tǒng)資源和降低程序的執(zhí)行效率.這時候我們一般通過自定義分頁來解
決這個問題.
DataGrid控件的AllowCustomPaging屬性用來獲取或設(shè)置DataGrid控件是否允許自定義分
頁;VirtualItemCoun屬性用來獲取或設(shè)置在使用自定義分頁時DataGrid中實際的項數(shù).要實現(xiàn)自定義分
頁,必須將AllowPaging與AllowCustomPaging屬性都設(shè)置為"True".
在DataGrid中要實現(xiàn)自定義分頁的關(guān)鍵是,使該控件僅僅調(diào)用當(dāng)前顯示所需要的數(shù)據(jù)源數(shù)據(jù),在下
面的例子中通過CurrentPageIndex和PageSize屬性的值,在數(shù)據(jù)綁定時只取當(dāng)前頁需要的數(shù)據(jù).
(1)頁面代碼:
復(fù)制代碼 代碼如下:
<%@ Page language="c#" Codebehind="Main.ASPx.cs" AutoEventWireup="false"
Inherits="SissonDemo.Main" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Main</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋體">
<ASP:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION:
absolute; TOP: 24px" runat="server"
Width="792px" Height="96px" AllowCustomPaging="True"
AllowPaging="True" PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>
</ASP:DataGrid></FONT>
</form>
</body>
</HTML>
(2)后臺代碼:
復(fù)制代碼 代碼如下:
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;
namespace SissonDemo
{
/**//// <summary>
/// Main 的摘要說明。
/// </summary>
public class Main : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
int startIndex = 0;//用來保存當(dāng)前頁數(shù)據(jù)項的起始索引
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind();//初始化時進(jìn)行數(shù)據(jù)綁定
}
}
void Bind()//綁定數(shù)據(jù)方法
{ //定義數(shù)據(jù)庫連接對象
SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
//創(chuàng)建數(shù)據(jù)適配對象
SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id
,price,pubdate from titles",cn);
//創(chuàng)建DataSet對象
DataSet ds=new DataSet();
try
{ //從指定的索引開始取PageSize條記錄.
da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
da.Fill(ds,"AllDataTable");//填充數(shù)據(jù)集合
//設(shè)置DataGrid控件實際要顯示的項數(shù)
DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;
//進(jìn)行數(shù)據(jù)綁定
DataGrid1.DataSource=ds.Tables["CurDataTable"];
DataGrid1.DataBind();
}
catch
{
Page.RegisterClientScriptBlock("","<script>alert('數(shù)據(jù)顯示錯
誤');</script>");
}
}
Web 窗體設(shè)計器生成的代碼#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEventHandler
(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//設(shè)置DataGrid當(dāng)前頁的索引值為用戶選擇的頁的索引
DataGrid1.CurrentPageIndex=e.NewPageIndex;
//取得當(dāng)前頁為止總共有多少條記錄,以便在就從該記錄開始讀取
startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;
//取得綁定數(shù)據(jù)
Bind();
}
}
}
在這段程序中,首先在數(shù)據(jù)綁定時,設(shè)置DataGrid控件的VirtualItemCoun屬性值為查詢結(jié)果集中的
記錄總數(shù),然后取得當(dāng)前頁要顯示的數(shù)據(jù),初始化時當(dāng)前頁顯示的數(shù)據(jù)為從取到的數(shù)據(jù)的零位置開始,到
DataGrid控件的PageSize屬性的設(shè)定值為止的數(shù)據(jù)記錄數(shù).在分頁操作中重新定義了取得數(shù)據(jù)時
的數(shù)據(jù)項的開始索引值,然后調(diào)用數(shù)據(jù)綁定方法把取到的新數(shù)據(jù)和DataGrid控件重新綁定.
AspNet技術(shù):如何在DataGrid控件中實現(xiàn)自定義分頁,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。