一个功能齐全的DataGrid分页例子

<xmp>C#版本 例子 DataGridPaging.aspx &amp;lt;%@ Page language=&amp;quot;c#&amp;quot; EnableViewState = &amp;quot;true&amp;quot; Codebehind=&amp;quot;DataGridPaging.aspx.cs&amp;quot; AutoEventWireup=&amp;quot;false&amp;quot; Inherits=&amp;quot;eMeng.Exam.DataGridPaging.DataGridPaging&amp;quot; %&amp;gt; &amp;lt;!DOCTYPE HTML PUBLIC &amp;quot;-//W3C//DTD HTML 4.0 Transitional//EN&amp;quot;&amp;gt; &amp;lt;HTML&amp;gt; &amp;lt;HEAD&amp;gt; &amp;lt;meta content=&amp;quot;Visual Basic 7.0&amp;quot; name=&amp;quot;CODE_LANGUAGE&amp;quot;&amp;gt; &amp;lt;meta content=&amp;quot;JavaScript&amp;quot; name=&amp;quot;vs_defaultClientScript&amp;quot;&amp;gt; &amp;lt;meta content=&amp;quot;http://schemas.microsoft.com/intellisense/ie5&amp;quot; name=&amp;quot;vs_targetSchema&amp;quot;&amp;gt; &amp;lt;/HEAD&amp;gt; &amp;lt;body MS_POSITIONING=&amp;quot;GridLayout&amp;quot;&amp;gt; &amp;lt;form id=&amp;quot;Form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;asp:datagrid id=&amp;quot;MyDataGrid&amp;quot; runat=&amp;quot;server&amp;quot; AutoGenerateColumns=&amp;quot;False&amp;quot; HorizontalAlign=&amp;quot;Center&amp;quot; AlternatingItemStyle-BackColor=&amp;quot;#eeeeee&amp;quot; HeaderStyle-BackColor=&amp;quot;#aaaadd&amp;quot; Font-Size=&amp;quot;8pt&amp;quot; Font-Name=&amp;quot;Verdana&amp;quot; CellSpacing=&amp;quot;0&amp;quot; CellPadding=&amp;quot;3&amp;quot; GridLines=&amp;quot;Both&amp;quot; BorderWidth=&amp;quot;1&amp;quot; BorderColor=&amp;quot;black&amp;quot; OnPageIndexChanged=&amp;quot;MyDataGrid_Page&amp;quot; PagerStyle-HorizontalAlign=&amp;quot;Right&amp;quot; PagerStyle-Mode=&amp;quot;NumericPages&amp;quot; PageSize=&amp;quot;5&amp;quot; AllowPaging=&amp;quot;True&amp;quot;&amp;gt; &amp;lt;AlternatingItemStyle BackColor=&amp;quot;#EEEEEE&amp;quot;&amp;gt;&amp;lt;/AlternatingItemStyle&amp;gt; &amp;lt;HeaderStyle BackColor=&amp;quot;#AAAADD&amp;quot; Font-Bold=&amp;quot;True&amp;quot; HorizontalAlign=&amp;quot;Center&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt; &amp;lt;PagerStyle HorizontalAlign=&amp;quot;Right&amp;quot; Mode=&amp;quot;NumericPages&amp;quot;&amp;gt;&amp;lt;/PagerStyle&amp;gt; &amp;lt;Columns&amp;gt; &amp;lt;asp:BoundColumn HeaderText=&amp;quot;标题&amp;quot; DataField=&amp;quot;Title&amp;quot; HeaderStyle-Width=&amp;quot;480px&amp;quot;&amp;gt; &amp;lt;/asp:BoundColumn&amp;gt; &amp;lt;asp:BoundColumn HeaderText=&amp;quot;发表日期&amp;quot; DataField=&amp;quot;CreateDate&amp;quot; DataFormatString=&amp;quot;{0:yyyy-MM-dd hh:mm:ss}&amp;quot;&amp;gt; &amp;lt;/asp:BoundColumn&amp;gt; &amp;lt;/Columns&amp;gt; &amp;lt;/asp:datagrid&amp;gt; &amp;lt;p style=&amp;quot;FONT-SIZE:9pt&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt; &amp;lt;asp:label id=&amp;quot;lblPageCount&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:label&amp;gt;&amp;amp;nbsp; &amp;lt;asp:label id=&amp;quot;lblCurrentIndex&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:label&amp;gt; &amp;lt;asp:linkbutton id=&amp;quot;btnFirst&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;0&amp;quot;&amp;gt;&amp;lt;/asp:linkbutton&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnPrev&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;prev&amp;quot;&amp;gt;&amp;lt;/asp:linkbutton&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnNext&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;next&amp;quot;&amp;gt;&amp;lt;/asp:linkbutton&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnLast&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;last&amp;quot;&amp;gt;&amp;lt;/asp:linkbutton&amp;gt; &amp;lt;/p&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/HTML&amp;gt; DataGridPaging.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.DataGridPaging { /// &amp;lt;summary&amp;gt; /// DataGridPaging 的摘要说明。 /// &amp;lt;/summary&amp;gt; public class DataGridPaging : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid MyDataGrid; protected System.Web.UI.WebControls.Label lblPageCount; protected System.Web.UI.WebControls.Label lblCurrentIndex; protected System.Web.UI.WebControls.LinkButton btnFirst; protected System.Web.UI.WebControls.LinkButton btnPrev; protected System.Web.UI.WebControls.LinkButton btnNext; protected System.Web.UI.WebControls.LinkButton btnLast; private OleDbConnection cn = new OleDbConnection(); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 btnFirst.Text = &amp;quot;最首页&amp;quot;; btnPrev.Text = &amp;quot;前一页&amp;quot;; btnNext.Text = &amp;quot;下一页&amp;quot;; btnLast.Text = &amp;quot;最后页&amp;quot;; OpenDatabase(); BindGrid(); } private void OpenDatabase() { cn.ConnectionString = &amp;quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&amp;quot; + Server.MapPath(&amp;quot;xxxx.mdb&amp;quot;); cn.Open(); } private void ShowStats() { lblCurrentIndex.Text = &amp;quot;第 &amp;quot; + (MyDataGrid.CurrentPageIndex + 1).ToString() + &amp;quot; 页&amp;quot;; lblPageCount.Text = &amp;quot;总共 &amp;quot; + MyDataGrid.PageCount.ToString() + &amp;quot; 页&amp;quot;; } public void PagerButtonClick(object sender, EventArgs e) { string arg = ((LinkButton)sender).CommandArgument.ToString(); switch(arg) { case &amp;quot;next&amp;quot;: if (MyDataGrid.CurrentPageIndex &amp;lt; (MyDataGrid.PageCount - 1)) { MyDataGrid.CurrentPageIndex += 1; } break; case &amp;quot;prev&amp;quot;: if (MyDataGrid.CurrentPageIndex &amp;gt; 0) { MyDataGrid.CurrentPageIndex -= 1; } break; case &amp;quot;last&amp;quot;: MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1); break; default: MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg); break; } BindGrid(); ShowStats(); } public void BindGrid() { OleDbConnection myConnection = cn; DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(&amp;quot;Select Title,CreateDate from Document&amp;quot;, myConnection); adapter.Fill(ds, &amp;quot;Document&amp;quot;); MyDataGrid.DataSource = ds.Tables[&amp;quot;Document&amp;quot;].DefaultView; MyDataGrid.DataBind(); ShowStats(); } public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e) { int startIndex ; startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize; MyDataGrid.CurrentPageIndex = e.NewPageIndex; BindGrid(); ShowStats(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// &amp;lt;summary&amp;gt; /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// &amp;lt;/summary&amp;gt; private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } } VB.NET版本 DataGridPaging.aspx &amp;lt;%@ Page Language=&amp;quot;vb&amp;quot; AutoEventWireup=&amp;quot;false&amp;quot; Codebehind=&amp;quot;DataGridPaging.aspx.vb&amp;quot; Inherits=&amp;quot;DataGridPaging&amp;quot;%&amp;gt; &amp;lt;!DOCTYPE HTML PUBLIC &amp;quot;-//W3C//DTD HTML 4.0 Transitional//EN&amp;quot;&amp;gt; &amp;lt;HTML&amp;gt; &amp;lt;HEAD&amp;gt; &amp;lt;meta content=&amp;quot;Visual Basic 7.0&amp;quot; name=&amp;quot;CODE_LANGUAGE&amp;quot;&amp;gt; &amp;lt;meta content=&amp;quot;JavaScript&amp;quot; name=&amp;quot;vs_defaultClientScript&amp;quot;&amp;gt; &amp;lt;meta content=&amp;quot;http://schemas.microsoft.com/intellisense/ie5&amp;quot; name=&amp;quot;vs_targetSchema&amp;quot;&amp;gt; &amp;lt;/HEAD&amp;gt; &amp;lt;body MS_POSITIONING=&amp;quot;GridLayout&amp;quot;&amp;gt; &amp;lt;form id=&amp;quot;Form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;asp:datagrid id=&amp;quot;MyDataGrid&amp;quot; runat=&amp;quot;server&amp;quot; AlternatingItemStyle-BackColor=&amp;quot;#eeeeee&amp;quot; HeaderStyle-BackColor=&amp;quot;#aaaadd&amp;quot; Font-Size=&amp;quot;8pt&amp;quot; Font-Name=&amp;quot;Verdana&amp;quot; CellSpacing=&amp;quot;0&amp;quot; CellPadding=&amp;quot;3&amp;quot; GridLines=&amp;quot;Both&amp;quot; BorderWidth=&amp;quot;1&amp;quot; BorderColor=&amp;quot;black&amp;quot; OnPageIndexChanged=&amp;quot;MyDataGrid_Page&amp;quot; PagerStyle-HorizontalAlign=&amp;quot;Right&amp;quot; PagerStyle-Mode=&amp;quot;NumericPages&amp;quot; PageSize=&amp;quot;5&amp;quot; AllowPaging=&amp;quot;True&amp;quot;&amp;gt; &amp;lt;AlternatingItemStyle BackColor=&amp;quot;#EEEEEE&amp;quot;&amp;gt;&amp;lt;/AlternatingItemStyle&amp;gt; &amp;lt;HeaderStyle BackColor=&amp;quot;#AAAADD&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt; &amp;lt;PagerStyle HorizontalAlign=&amp;quot;Right&amp;quot; Mode=&amp;quot;NumericPages&amp;quot;&amp;gt;&amp;lt;/PagerStyle&amp;gt; &amp;lt;/asp:datagrid&amp;gt; &amp;lt;p style=&amp;quot;font-size:9pt&amp;quot;&amp;gt; &amp;lt;asp:label id=&amp;quot;lblPageCount&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:label&amp;gt;&amp;amp;nbsp; &amp;lt;asp:label id=&amp;quot;lblCurrentIndex&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:label&amp;gt; &amp;lt;asp:linkbutton id=&amp;quot;btnFirst&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;0&amp;quot;&amp;gt; &amp;lt;/asp:linkbutton&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnPrev&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;prev&amp;quot;&amp;gt; &amp;lt;/asp:linkbutton&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnNext&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;next&amp;quot;&amp;gt; &amp;lt;/asp:linkbutton&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnLast&amp;quot; onclick=&amp;quot;PagerButtonClick&amp;quot; runat=&amp;quot;server&amp;quot; Font-Name=&amp;quot;verdana&amp;quot; Font-size=&amp;quot;8pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;last&amp;quot;&amp;gt; &amp;lt;/asp:linkbutton&amp;gt; &amp;lt;/p&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/HTML&amp;gt; DataGridPaging.aspx.vb Imports System.Data.SqlClient Imports System.Data Imports System.Web.UI Public Class DataGridPaging Inherits System.Web.UI.Page Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton Protected WithEvents btnPrev As System.Web.UI.WebControls.LinkButton Protected WithEvents btnNext As System.Web.UI.WebControls.LinkButton Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid #Region &amp;quot; Web Form Designer Generated Code &amp;quot; 'This call is required by the Web Form Designer. &amp;lt;System.Diagnostics.DebuggerStepThrough()&amp;gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MyBase.Load btnFirst.Text = &amp;quot;最首页&amp;quot; btnPrev.Text = &amp;quot;前一页&amp;quot; btnNext.Text = &amp;quot;下一页&amp;quot; btnLast.Text = &amp;quot;最后页&amp;quot; OpenDatabase() BindGrid() End Sub Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Dim startIndex As Integer startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize MyDataGrid.CurrentPageIndex = e.NewPageIndex BindGrid() ShowStats() End Sub Sub BindGrid() Dim myConnection As SqlConnection = cn Dim ds As DataSet = New DataSet() Dim adapter As SqlDataAdapter = New SqlDataAdapter(&amp;quot;Select * from Orders&amp;quot;, myConnection) adapter.Fill(ds, &amp;quot;Orders&amp;quot;) MyDataGrid.DataSource = ds.Tables(&amp;quot;Orders&amp;quot;).DefaultView MyDataGrid.DataBind() ShowStats() End Sub Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs) 'used by external paging UI Dim arg As String = sender.CommandArgument Select Case arg Case &amp;quot;next&amp;quot; If (MyDataGrid.CurrentPageIndex &amp;lt; (MyDataGrid.PageCount - 1)) Then MyDataGrid.CurrentPageIndex += 1 End If Case &amp;quot;prev&amp;quot; If (MyDataGrid.CurrentPageIndex &amp;gt; 0) Then MyDataGrid.CurrentPageIndex -= 1 End If Case &amp;quot;last&amp;quot; MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1) Case Else 'page number MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg) End Select BindGrid() ShowStats() End Sub Sub ShowStats() lblCurrentIndex.Text = &amp;quot;第 &amp;quot; + (MyDataGrid.CurrentPageIndex + 1).ToString() + &amp;quot; 页&amp;quot; lblPageCount.Text = &amp;quot;总共 &amp;quot; + MyDataGrid.PageCount.ToString() + &amp;quot; 页&amp;quot; End Sub Public cn As New SqlClient.SqlConnection() Public Sub OpenDatabase() cn.ConnectionString = &amp;quot;Server=.;Database=NorthWind;User Id=sa;Password=;&amp;quot; cn.Open() End Sub End Class -------------------------------------------------------------------------------- 方法2:完全后代码写法:例子 DataGridPaging2.aspx &amp;lt;%@ Page language=&amp;quot;c#&amp;quot; EnableViewState = &amp;quot;true&amp;quot; Codebehind=&amp;quot;DataGridPaging2.aspx.cs&amp;quot; AutoEventWireup=&amp;quot;false&amp;quot; Inherits=&amp;quot;eMeng.Exam.DataGridPaging2.DataGridPaging2&amp;quot; %&amp;gt; &amp;lt;!DOCTYPE HTML PUBLIC &amp;quot;-//W3C//DTD HTML 4.0 Transitional//EN&amp;quot;&amp;gt; &amp;lt;HTML&amp;gt; &amp;lt;HEAD&amp;gt; &amp;lt;TITLE&amp;gt;功能齐全的分页的例子【2】&amp;lt;/TITLE&amp;gt; &amp;lt;meta content=&amp;quot;Visual Basic 7.0&amp;quot; name=&amp;quot;CODE_LANGUAGE&amp;quot;&amp;gt; &amp;lt;meta content=&amp;quot;JavaScript&amp;quot; name=&amp;quot;vs_defaultClientScript&amp;quot;&amp;gt; &amp;lt;meta content=&amp;quot;http://schemas.microsoft.com/intellisense/ie5&amp;quot; name=&amp;quot;vs_targetSchema&amp;quot;&amp;gt; &amp;lt;/HEAD&amp;gt; &amp;lt;body MS_POSITIONING=&amp;quot;GridLayout&amp;quot;&amp;gt; &amp;lt;form id=&amp;quot;Form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;asp:datagrid id=&amp;quot;MyDataGrid&amp;quot; runat=&amp;quot;server&amp;quot; AutoGenerateColumns=&amp;quot;False&amp;quot; HorizontalAlign=&amp;quot;Center&amp;quot; AlternatingItemStyle-BackColor=&amp;quot;#eeeeee&amp;quot; HeaderStyle-BackColor=&amp;quot;#aaaadd&amp;quot; Font-Size=&amp;quot;8pt&amp;quot; Font-Name=&amp;quot;Verdana&amp;quot; CellSpacing=&amp;quot;0&amp;quot; CellPadding=&amp;quot;3&amp;quot; GridLines=&amp;quot;Both&amp;quot; BorderWidth=&amp;quot;1&amp;quot; BorderColor=&amp;quot;black&amp;quot; PagerStyle-HorizontalAlign=&amp;quot;Right&amp;quot; PagerStyle-Mode=&amp;quot;NumericPages&amp;quot; PageSize=&amp;quot;5&amp;quot; AllowPaging=&amp;quot;True&amp;quot;&amp;gt; &amp;lt;AlternatingItemStyle BackColor=&amp;quot;#EEEEEE&amp;quot;&amp;gt;&amp;lt;/AlternatingItemStyle&amp;gt; &amp;lt;HeaderStyle BackColor=&amp;quot;#AAAADD&amp;quot; Font-Bold=&amp;quot;True&amp;quot; HorizontalAlign=&amp;quot;Center&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt; &amp;lt;PagerStyle HorizontalAlign=&amp;quot;Right&amp;quot; Mode=&amp;quot;NumericPages&amp;quot;&amp;gt;&amp;lt;/PagerStyle&amp;gt; &amp;lt;Columns&amp;gt; &amp;lt;asp:BoundColumn HeaderText=&amp;quot;标题&amp;quot; DataField=&amp;quot;Title&amp;quot; HeaderStyle-Width=&amp;quot;480px&amp;quot;&amp;gt;&amp;lt;/asp:BoundColumn&amp;gt; &amp;lt;asp:BoundColumn HeaderText=&amp;quot;发表日期&amp;quot; DataField=&amp;quot;CreateDate&amp;quot; DataFormatString=&amp;quot;{0:yyyy-MM-dd hh:mm:ss}&amp;quot;&amp;gt;&amp;lt;/asp:BoundColumn&amp;gt; &amp;lt;/Columns&amp;gt; &amp;lt;/asp:datagrid&amp;gt; &amp;lt;p align=&amp;quot;center&amp;quot;&amp;gt; &amp;lt;asp:label id=&amp;quot;lblPageCount&amp;quot; runat=&amp;quot;server&amp;quot; Font-Size=&amp;quot;9pt&amp;quot; Font-Bold=&amp;quot;True&amp;quot;&amp;gt;&amp;lt;/asp:label&amp;gt;&amp;amp;nbsp; &amp;lt;asp:label id=&amp;quot;lblCurrentIndex&amp;quot; runat=&amp;quot;server&amp;quot; Font-Size=&amp;quot;9pt&amp;quot; Font-Bold=&amp;quot;True&amp;quot;&amp;gt;&amp;lt;/asp:label&amp;gt; &amp;lt;asp:linkbutton id=&amp;quot;btnFirst&amp;quot; runat=&amp;quot;server&amp;quot; Font-size=&amp;quot;9pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;0&amp;quot;/&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnPrev&amp;quot; runat=&amp;quot;server&amp;quot; Font-size=&amp;quot;9pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;prev&amp;quot;/&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnNext&amp;quot; runat=&amp;quot;server&amp;quot; Font-size=&amp;quot;9pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;next&amp;quot;/&amp;gt;&amp;amp;nbsp; &amp;lt;asp:linkbutton id=&amp;quot;btnLast&amp;quot; runat=&amp;quot;server&amp;quot; Font-size=&amp;quot;9pt&amp;quot; ForeColor=&amp;quot;navy&amp;quot; CommandArgument=&amp;quot;last&amp;quot;/&amp;gt; &amp;lt;/p&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/HTML&amp;gt; DataGridPaging2.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.DataGridPaging2 { /// &amp;lt;summary&amp;gt; /// DataGridPaging 的摘要说明。 /// &amp;lt;/summary&amp;gt; public class DataGridPaging2 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid MyDataGrid; protected System.Web.UI.WebControls.Label lblPageCount; protected System.Web.UI.WebControls.Label lblCurrentIndex; protected System.Web.UI.WebControls.LinkButton btnFirst; protected System.Web.UI.WebControls.LinkButton btnPrev; protected System.Web.UI.WebControls.LinkButton btnNext; protected System.Web.UI.WebControls.LinkButton btnLast; private OleDbConnection cn = new OleDbConnection(); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 btnFirst.Text = &amp;quot;最首页&amp;quot;; btnPrev.Text = &amp;quot;前一页&amp;quot;; btnNext.Text = &amp;quot;下一页&amp;quot;; btnLast.Text = &amp;quot;最后页&amp;quot;; OpenDatabase(); BindGrid(); } private void OpenDatabase() { cn.ConnectionString = &amp;quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&amp;quot; + HttpContext.Current.Server.MapPath(&amp;quot;../../aspxWeb.mdb.ascx&amp;quot;); cn.Open(); } private void ShowStats() { lblCurrentIndex.Text = &amp;quot;第 &amp;quot; + (MyDataGrid.CurrentPageIndex + 1).ToString() + &amp;quot; 页&amp;quot;; lblPageCount.Text = &amp;quot;总共 &amp;quot; + MyDataGrid.PageCount.ToString() + &amp;quot; 页&amp;quot;; } public void PagerButtonClick(object sender, EventArgs e) { string arg = ((LinkButton)sender).CommandArgument.ToString(); switch(arg) { case &amp;quot;next&amp;quot;: if (MyDataGrid.CurrentPageIndex &amp;lt; (MyDataGrid.PageCount - 1)) { MyDataGrid.CurrentPageIndex += 1; } break; case &amp;quot;prev&amp;quot;: if (MyDataGrid.CurrentPageIndex &amp;gt; 0) { MyDataGrid.CurrentPageIndex -= 1; } break; case &amp;quot;last&amp;quot;: MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1); break; default: MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg); break; } BindGrid(); ShowStats(); } public void BindGrid() { OleDbConnection myConnection = cn; DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(&amp;quot;Select Title,CreateDate from Document&amp;quot;, myConnection); adapter.Fill(ds, &amp;quot;Document&amp;quot;); MyDataGrid.DataSource = ds.Tables[&amp;quot;Document&amp;quot;].DefaultView; MyDataGrid.DataBind(); ShowStats(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// &amp;lt;summary&amp;gt; /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// &amp;lt;/summary&amp;gt; private void InitializeComponent() { this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.OnPageIndexChanged); this.btnFirst.Click += new System.EventHandler(this.PagerButtonClick); this.btnPrev.Click += new System.EventHandler(this.PagerButtonClick); this.btnNext.Click += new System.EventHandler(this.PagerButtonClick); this.btnLast.Click += new System.EventHandler(this.PagerButtonClick); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void OnPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { int startIndex ; startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize; MyDataGrid.CurrentPageIndex = e.NewPageIndex; BindGrid(); ShowStats(); } } } </xmp>

你可能感兴趣的:(UI,Web,asp.net,asp,vb)