<%@ 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ù)庫連接對象
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í)際要顯示的項(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);}
#endregionprivate 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控件重新綁定.聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com