面的这个例子提供了DataGrid的分页功能。aspx代码和Cohe Behind代码分别如下,注意更改数据录连接字符串。结果如下:
\n
\n
C#版本 例子
\n
DataGridPaging.aspx
\n\nDataGridPaging.aspx.cs
using System;\nusing System.Collections;
\nusing System.ComponentModel;
\nusing System.Data;
\nusing System.Data.OleDb;
\nusing System.Drawing;
\nusing System.Web;
\nusing System.Web.SessionState;
\nusing System.Web.UI;
\nusing System.Web.UI.WebControls;
\nusing System.Web.UI.HtmlControls;\n
namespace eMeng.Exam.DataGridPaging
\n{
\n///
\n
\n///
public class DataGridPaging : System.Web.UI.Page
\n{
\n protected System.Web.UI.WebControls.DataGrid MyDataGrid;
\n protected System.Web.UI.WebControls.Label lblPageCount;
\n protected System.Web.UI.WebControls.Label lblCurrentIndex;
\n protected System.Web.UI.WebControls.LinkButton btnFirst;
\n protected System.Web.UI.WebControls.LinkButton btnPrev;
\n protected System.Web.UI.WebControls.LinkButton btnNext;
\n protected System.Web.UI.WebControls.LinkButton btnLast;
\n private OleDbConnection cn = new OleDbConnection();
private void Page_Load(object sender, System.EventArgs e)
\n{
\n // 在此处放置用户代码以初始化页面
\n btnFirst.Text = “最首页”;
\n btnPrev.Text = “前一页”;
\n btnNext.Text = “下一页”;
\n btnLast.Text = “最后页”;
\n OpenDatabase();
\n BindGrid();
\n}
\nprivate void OpenDatabase()
\n{
\n cn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + Server.MapPath(“xxxx.mdb”);
\n cn.Open();
\n}
\nprivate void ShowStats()
\n{
\n lblCurrentIndex.Text = “第 ” + (MyDataGrid.CurrentPageIndex + 1).ToString() + ” 页”;
\n lblPageCount.Text = “总共 ” + MyDataGrid.PageCount.ToString() + ” 页”;
\n}
public void PagerButtonClick(object sender, EventArgs e)
\n{
\n string arg = ((LinkButton)sender).CommandArgument.ToString();
\n switch(arg)
\n {
\n case “next”:
\n if (MyDataGrid.CurrentPageIndex 0)
\n {
\n MyDataGrid.CurrentPageIndex -= 1;
\n }
\n break;
\n case “last”:
\n MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount – 1);
\n break;
\n default:
\n MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
\n break;
\n }
\n BindGrid();
\n ShowStats();
\n}
\npublic void BindGrid()
\n{
\n OleDbConnection myConnection = cn;
\n DataSet ds = new DataSet();
\n OleDbDataAdapter adapter = new OleDbDataAdapter(“Select Title,CreateDate from Document”, myConnection);
\n adapter.Fill(ds, “Document”);
\n MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
\n MyDataGrid.DataBind();
\n ShowStats();
\n}
\npublic void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
\n{
\n int startIndex ;
\n startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
\n MyDataGrid.CurrentPageIndex = e.NewPageIndex;
\n BindGrid();
\n ShowStats();
\n}
#region Web Form Designer generated code
\noverride protected void OnInit(EventArgs e)
\n{
\n//
\n// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
\n//
\nInitializeComponent();
\nbase.OnInit(e);
\n}
///
\n
\n/// 此方法的内容。
\n///
private void InitializeComponent()
\n{
\nthis.Load += new System.EventHandler(this.Page_Load);
}
\n#endregion
\n}
\n}
VB.NET版本
\n
DataGridPaging.aspx
\n\nDataGridPaging.aspx.vb
Imports System.Data.SqlClient\nImports System.Data
\nImports System.Web.UI\n
Public Class DataGridPaging
\n Inherits System.Web.UI.Page
Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
\n Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton
\n Protected WithEvents btnPrev As System.Web.UI.WebControls.LinkButton
\n Protected WithEvents btnNext As System.Web.UI.WebControls.LinkButton
\n Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton
\n Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label
\n Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
\n Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region ” Web Form Designer Generated Code ”
\n ‘This call is required by the Web Form Designer.
\n Private Sub InitializeComponent()
End Sub
\n Private Sub Page_Init(ByVal sender As System.Object,_
\n ByVal e As System.EventArgs) Handles MyBase.Init
\n ‘CODEGEN: This method call is required by the Web Form Designer
\n ‘Do not modify it using the code editor.
\n InitializeComponent()
\n End Sub
#End Region
\n Private Sub Page_Load(ByVal sender As System.Object,_
\n ByVal e As System.EventArgs) Handles MyBase.Load
\n btnFirst.Text = “最首页”
\n btnPrev.Text = “前一页”
\n btnNext.Text = “下一页”
\n btnLast.Text = “最后页”
\n OpenDatabase()
\n BindGrid()
\n End Sub
Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
\n Dim startIndex As Integer
\n startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
\n MyDataGrid.CurrentPageIndex = e.NewPageIndex
\n BindGrid()
\n ShowStats()
\n End Sub
Sub BindGrid()
\n Dim myConnection As SqlConnection = cn
\n Dim ds As DataSet = New DataSet()
\n Dim adapter As SqlDataAdapter = New SqlDataAdapter(“Select * from Orders”, myConnection)
\n adapter.Fill(ds, “Orders”)
\n MyDataGrid.DataSource = ds.Tables(“Orders”).DefaultView
\n MyDataGrid.DataBind()
\n ShowStats()
End Sub
\n Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
\n ‘used by external paging UI
\n Dim arg As String = sender.CommandArgument
Select Case arg
\n Case “next”
\n If (MyDataGrid.CurrentPageIndex 0) Then
\n MyDataGrid.CurrentPageIndex -= 1
\n End If
\n Case “last”
\n MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount – 1)
\n Case Else
\n ‘page number
\n MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg)
\n End Select
\n BindGrid()
\n ShowStats()
\n End Sub
Sub ShowStats()
\n lblCurrentIndex.Text = “第 ” + (MyDataGrid.CurrentPageIndex + 1).ToString() + ” 页”
\n lblPageCount.Text = “总共 ” + MyDataGrid.PageCount.ToString() + ” 页”
\n End Sub
Public cn As New SqlClient.SqlConnection()
\n Public Sub OpenDatabase()
\n cn.ConnectionString = “Server=.;Database=NorthWind;User Id=sa;Password=;”
\n cn.Open()
\n End Sub
\nEnd Class
\n
方法2:完全后代码写法:例子
\n
DataGridPaging2.aspx
\n
DataGridPaging2.aspx.cs
using System;\nusing System.Collections;
\nusing System.ComponentModel;
\nusing System.Data;
\nusing System.Data.OleDb;
\nusing System.Drawing;
\nusing System.Web;
\nusing System.Web.SessionState;
\nusing System.Web.UI;
\nusing System.Web.UI.WebControls;
\nusing System.Web.UI.HtmlControls;\n
namespace eMeng.Exam.DataGridPaging2
\n{
\n///
\n
\n///
public class DataGridPaging2 : System.Web.UI.Page
\n{
\n protected System.Web.UI.WebControls.DataGrid MyDataGrid;
\n protected System.Web.UI.WebControls.Label lblPageCount;
\n protected System.Web.UI.WebControls.Label lblCurrentIndex;
\n protected System.Web.UI.WebControls.LinkButton btnFirst;
\n protected System.Web.UI.WebControls.LinkButton btnPrev;
\n protected System.Web.UI.WebControls.LinkButton btnNext;
\n protected System.Web.UI.WebControls.LinkButton btnLast;
\n private OleDbConnection cn = new OleDbConnection();
private void Page_Load(object sender, System.EventArgs e)
\n {
\n // 在此处放置用户代码以初始化页面
\n btnFirst.Text = “最首页”;
\n btnPrev.Text = “前一页”;
\n btnNext.Text = “下一页”;
\n btnLast.Text = “最后页”;
\n OpenDatabase();
\n BindGrid();
\n }
\n private void OpenDatabase()
\n {
\n cn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=”
\n + HttpContext.Current.Server.MapPath(“../../aspxWeb.mdb.ascx”);
\n cn.Open();
\n }
\n private void ShowStats()
\n {
\n lblCurrentIndex.Text = “第 ” + (MyDataGrid.CurrentPageIndex + 1).ToString() + ” 页”;
\n lblPageCount.Text = “总共 ” + MyDataGrid.PageCount.ToString() + ” 页”;
\n }
public void PagerButtonClick(object sender, EventArgs e)
\n {
\n string arg = ((LinkButton)sender).CommandArgument.ToString();
\n switch(arg)
\n {
\n case “next”:
\n if (MyDataGrid.CurrentPageIndex 0)
\n {
\n MyDataGrid.CurrentPageIndex -= 1;
\n }
\n break;
\n case “last”:
\n MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount – 1);
\n break;
\n default:
\n MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
\n break;
\n }
\n BindGrid();
\n ShowStats();
\n }
\n public void BindGrid()
\n {
\n OleDbConnection myConnection = cn;
\n DataSet ds = new DataSet();
\n OleDbDataAdapter adapter = new OleDbDataAdapter(“Select Title,CreateDate from Document”, myConnection);
\n adapter.Fill(ds, “Document”);
\n MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
\n MyDataGrid.DataBind();
\n ShowStats();
\n }
#region Web Form Designer generated code
\n override protected void OnInit(EventArgs e)
\n {
\n //
\n // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
\n //
\n InitializeComponent();
\n base.OnInit(e);
\n }
///
\n
\n /// 此方法的内容。
\n ///
private void InitializeComponent()
\n {
\n this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.OnPageIndexChanged);
\n this.btnFirst.Click += new System.EventHandler(this.PagerButtonClick);
\n this.btnPrev.Click += new System.EventHandler(this.PagerButtonClick);
\n this.btnNext.Click += new System.EventHandler(this.PagerButtonClick);
\n this.btnLast.Click += new System.EventHandler(this.PagerButtonClick);
\n this.Load += new System.EventHandler(this.Page_Load);
}
\n#endregion
private void OnPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
\n {
\n int startIndex ;
\n startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
\n MyDataGrid.CurrentPageIndex = e.NewPageIndex;
\n BindGrid();
\n ShowStats();
\n }
\n}
\n}
\n作者:孟宪会 出自:【孟宪会之精彩世界】