如何在DataGrid控件中實(shí)現(xiàn)自定義分頁
在一般情況下,DataGrid控件每次實(shí)現(xiàn)翻頁操作時(shí),都會(huì)將數(shù)據(jù)源中的數(shù)據(jù)重新調(diào)用一次,當(dāng)數(shù)據(jù)中
數(shù)據(jù)很多時(shí),這樣做就會(huì)很浪費(fèi)系統(tǒng)資源和降低程序的執(zhí)行效率.這時(shí)候我們一般通過自定義分頁來解
決這個(gè)問題.
DataGrid控件的AllowCustomPaging屬性用來獲取或設(shè)置DataGrid控件是否允許自定義分
頁;VirtualItemCoun屬性用來獲取或設(shè)置在使用自定義分頁時(shí)DataGrid中實(shí)際的項(xiàng)數(shù).要實(shí)現(xiàn)自定義分
頁,必須將AllowPaging與AllowCustomPaging屬性都設(shè)置為"True".
在DataGrid中要實(shí)現(xiàn)自定義分頁的關(guān)鍵是,使該控件僅僅調(diào)用當(dāng)前顯示所需要的數(shù)據(jù)源數(shù)據(jù),在下
面的例子中通過CurrentPageIndex和PageSize屬性的值,在數(shù)據(jù)綁定時(shí)只取當(dāng)前頁需要的數(shù)據(jù).
(1)頁面代碼:
<%@ 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)后臺(tái)代碼:
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ù)項(xiàng)的起始索引
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind();//初始化時(shí)進(jìn)行數(shù)據(jù)綁定
}
}
void Bind()//綁定數(shù)據(jù)方法
{ //定義數(shù)據(jù)庫連接對(duì)象
SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
//創(chuàng)建數(shù)據(jù)適配對(duì)象
SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id
,price,pubdate from titles",cn);
//創(chuàng)建DataSet對(duì)象
DataSet ds=new DataSet();
try
{ //從指定的索引開始取PageSize條記錄.
da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
da.Fill(ds,"AllDataTable");//填充數(shù)據(jù)集合
//設(shè)置DataGrid控件實(shí)際要顯示的項(xiàng)數(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ù)顯示錯(cuò)
誤');</script>");
}
}
Web 窗體設(shè)計(jì)器生成的代碼#region Web 窗體設(shè)計(jì)器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(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í),設(shè)置DataGrid控件的VirtualItemCoun屬性值為查詢結(jié)果集中的
記錄總數(shù),然后取得當(dāng)前頁要顯示的數(shù)據(jù),初始化時(shí)當(dāng)前頁顯示的數(shù)據(jù)為從取到的數(shù)據(jù)的零位置開始,到
DataGrid控件的PageSize屬性的設(shè)定值為止的數(shù)據(jù)記錄數(shù).在分頁操作中重新定義了取得下一頁數(shù)據(jù)時(shí)
的數(shù)據(jù)項(xiàng)的開始索引值,然后調(diào)用數(shù)據(jù)綁定方法把取到的新數(shù)據(jù)和DataGrid控件重新綁定.
- DataGrid同時(shí)具有分頁和排序功能及注意點(diǎn)
- 讓Asp.NET的DataGrid可排序、可選擇、可分頁
- 讓Asp.NET的DataGrid可排序、可選擇、可分頁
- 一個(gè)典型的PHP分頁實(shí)例代碼分享
- jQuery EasyUI API 中文文檔 - Pagination分頁
- 高效的SQLSERVER分頁查詢(推薦)
- EasyUi datagrid 實(shí)現(xiàn)表格分頁
- jQuery EasyUI datagrid實(shí)現(xiàn)本地分頁的方法
- EasyUi中的Combogrid 實(shí)現(xiàn)分頁和動(dòng)態(tài)搜索遠(yuǎn)程數(shù)據(jù)
- jQuery EasyUI Pagination實(shí)現(xiàn)分頁的常用方法
- SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁顯示
相關(guān)文章
解讀ASP.NET 5 & MVC6系列教程(4):核心技術(shù)與環(huán)境配置
這篇文章主要介紹了ASP.NET 5 核心技術(shù)與環(huán)境配置,需要的朋友可以參考下2016-06-06在ASP.NET 2.0中操作數(shù)據(jù)之五十七:在分層架構(gòu)中緩存數(shù)據(jù)
上一篇文章我們介紹了ASP.NET 2.0中使用ObjectDataSource在視圖層緩存數(shù)據(jù),缺點(diǎn)是不言而喻的,為了達(dá)到低耦合,本文介紹如何在三層架構(gòu)中使用緩存技術(shù)來緩存數(shù)據(jù)。2016-05-05Mac中體驗(yàn)ASP.NET 5 beta2的K gen代碼生成
這篇文章主要介紹了Mac中體驗(yàn)ASP.NET 5 beta2的K gen代碼生成,需要的朋友可以參考一下。2016-06-06NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作
本文介紹IStartupTask,該類會(huì)在系統(tǒng)啟動(dòng)時(shí)執(zhí)行,IStartupTask調(diào)用IEfDataProvider進(jìn)行數(shù)據(jù)庫的初始化。2016-04-04[翻譯]Scott Mitchell 的ASP.NET 2.0數(shù)據(jù)教程
本文主要是對(duì)Scott Mitchell 的ASP.NET 2.0數(shù)據(jù)系列教程的一個(gè)索引的整理,方便大家查看全部的教程。2016-05-05《解剖PetShop》之六:PetShop之表示層設(shè)計(jì)
表示層(Presentation Layer)通俗的講就是界面層,也就是展示給客戶的網(wǎng)頁,本文主要講解PetShop4.0的表示層設(shè)計(jì),需要的朋友可以參考下。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之七十:配置數(shù)據(jù)庫連接和命令等級(jí)設(shè)置
TableAdapters將數(shù)據(jù)訪問等細(xì)節(jié)進(jìn)行的封裝,但是默認(rèn)情況下這些屬性要么標(biāo)記為internal要么為private,我們可以使用部分類,在部分類里使用標(biāo)記為public的方法或?qū)傩浴?/div> 2016-05-05NopCommerce架構(gòu)分析(一)Autofac依賴注入類生成容器
本文介紹了NopCommerce中IOC框架Autofac的使用,Autofac是一款I(lǐng)OC框架,比較于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很輕量級(jí)性能上非常高。2016-04-04最新評(píng)論