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

发表于:2007-06-30来源:作者:点击数: 标签:
%@ Page language=c# EnableViewState = true Codebehind=DataGridPaging.aspx.cs AutoEventWireup=false Inherits=eMeng.Exam.DataGridPaging.DataGridPaging %!DOCTYPE HTML PUBLIC -//W3C//D TD HTML 4.0 Transitional//ENHTMLHEADmeta content=Visual Ba
&lt;%@ Page language=&quot;c#&quot; EnableViewState = &quot;true&quot; Codebehind=&quot;DataGridPaging.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;eMeng.Exam.DataGridPaging.DataGridPaging&quot; %&gt; &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//D<STRONG><A href="http://www.ltesting.net/html/11/category-catid-111.html" target="_blank" >TD</A></STRONG> HTML 4.0 Transitional//EN&quot;&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;meta content=&quot;Visual Basic 7.0&quot; name=&quot;CODE_LANGUAGE&quot;&gt; &lt;meta content=&quot;<STRONG><A href="http://www.ltesting.net/html/54/category-catid-154.html" target="_blank" >Java</A></STRONG>Script&quot; name=&quot;vs_defaultClientScript&quot;&gt; &lt;meta content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot; name=&quot;vs_targetSchema&quot;&gt; &lt;/HEAD&gt; &lt;body MS_POSITIONING=&quot;GridLayout&quot;&gt; &lt;form id=&quot;Form1&quot; runat=&quot;server&quot;&gt; &lt;asp:datagrid id=&quot;MyDataGrid&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; HorizontalAlign=&quot;Center&quot; AlternatingItemStyle-BackColor=&quot;#eeeeee&quot; HeaderStyle-BackColor=&quot;#aaaadd&quot; Font-Size=&quot;8pt&quot; Font-Name=&quot;Verdana&quot; CellSpacing=&quot;0&quot; CellPadding=&quot;3&quot; GridLines=&quot;Both&quot; BorderWidth=&quot;1&quot; BorderColor=&quot;black&quot; OnPageIndexChanged=&quot;MyDataGrid_Page&quot; PagerStyle-HorizontalAlign=&quot;Right&quot; PagerStyle-Mode=&quot;NumericPages&quot; PageSize=&quot;5&quot; AllowPaging=&quot;True&quot;&gt; &lt;AlternatingItemStyle BackColor=&quot;#EEEEEE&quot;&gt;&lt;/AlternatingItemStyle&gt; &lt;HeaderStyle BackColor=&quot;#AAAADD&quot; Font-Bold=&quot;True&quot; HorizontalAlign=&quot;Center&quot;&gt;&lt;/HeaderStyle&gt; &lt;PagerStyle HorizontalAlign=&quot;Right&quot; Mode=&quot;NumericPages&quot;&gt;&lt;/PagerStyle&gt; &lt;Columns&gt; &lt;asp:BoundColumn HeaderText=&quot;标题&quot; DataField=&quot;Title&quot; HeaderStyle-Width=&quot;480px&quot;&gt; &lt;/asp:BoundColumn&gt; &lt;asp:BoundColumn HeaderText=&quot;发表日期&quot; DataField=&quot;CreateDate&quot; DataFormatString=&quot;{0:yyyy-MM-dd hh:mm:ss}&quot;&gt; &lt;/asp:BoundColumn&gt; &lt;/Columns&gt; &lt;/asp:datagrid&gt; &lt;p style=&quot;FONT-SIZE:9pt&quot; align=&quot;center&quot;&gt; &lt;asp:label id=&quot;lblPageCount&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt;&amp;nbsp; &lt;asp:label id=&quot;lblCurrentIndex&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt; &lt;asp:linkbutton id=&quot;btnFirst&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;0&quot;&gt;&lt;/asp:linkbutton&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnPrev&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;prev&quot;&gt;&lt;/asp:linkbutton&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnNext&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;next&quot;&gt;&lt;/asp:linkbutton&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnLast&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;last&quot;&gt;&lt;/asp:linkbutton&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/HTML&gt;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 { /// &lt;summary&gt; /// DataGridPaging 的摘要说明。 /// &lt;/summary&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 = &quot;最首页&quot;; btnPrev.Text = &quot;前一页&quot;; btnNext.Text = &quot;下一页&quot;; btnLast.Text = &quot;最后页&quot;; OpenDatabase(); BindGrid(); } private void OpenDatabase() { cn.ConnectionString = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; + Server.MapPath(&quot;xxxx.mdb&quot;); cn.Open(); } private void ShowStats() { lblCurrentIndex.Text = &quot;第 &quot; + (MyDataGrid.CurrentPageIndex + 1).ToString() + &quot; 页&quot;; lblPageCount.Text = &quot;总共 &quot; + MyDataGrid.PageCount.ToString() + &quot; 页&quot;; } public void PagerButtonClick(object sender, EventArgs e) { string arg = ((LinkButton)sender).CommandArgument.ToString(); switch(arg) { case &quot;next&quot;: if (MyDataGrid.CurrentPageIndex &lt; (MyDataGrid.PageCount - 1)) { MyDataGrid.CurrentPageIndex += 1; } break; case &quot;prev&quot;: if (MyDataGrid.CurrentPageIndex &gt; 0) { MyDataGrid.CurrentPageIndex -= 1; } break; case &quot;last&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(&quot;Select Title,CreateDate from Document&quot;, myConnection); adapter.Fill(ds, &quot;Document&quot;); MyDataGrid.DataSource = ds.Tables[&quot;Document&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); } /// &lt;summary&gt; /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// &lt;/summary&gt; private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } } 完全后台代码&lt;%@ Page language=&quot;c#&quot; EnableViewState = &quot;true&quot; Codebehind=&quot;DataGridPaging2.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;eMeng.Exam.DataGridPaging2.DataGridPaging2&quot; %&gt; &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;功能齐全的分页的例子【2】&lt;/TITLE&gt; &lt;meta content=&quot;Visual Basic 7.0&quot; name=&quot;CODE_LANGUAGE&quot;&gt; &lt;meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;&gt; &lt;meta content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot; name=&quot;vs_targetSchema&quot;&gt; &lt;/HEAD&gt; &lt;body MS_POSITIONING=&quot;GridLayout&quot;&gt; &lt;form id=&quot;Form1&quot; runat=&quot;server&quot;&gt; &lt;asp:datagrid id=&quot;MyDataGrid&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; HorizontalAlign=&quot;Center&quot; AlternatingItemStyle-BackColor=&quot;#eeeeee&quot; HeaderStyle-BackColor=&quot;#aaaadd&quot; Font-Size=&quot;8pt&quot; Font-Name=&quot;Verdana&quot; CellSpacing=&quot;0&quot; CellPadding=&quot;3&quot; GridLines=&quot;Both&quot; BorderWidth=&quot;1&quot; BorderColor=&quot;black&quot; PagerStyle-HorizontalAlign=&quot;Right&quot; PagerStyle-Mode=&quot;NumericPages&quot; PageSize=&quot;5&quot; AllowPaging=&quot;True&quot;&gt; &lt;AlternatingItemStyle BackColor=&quot;#EEEEEE&quot;&gt;&lt;/AlternatingItemStyle&gt; &lt;HeaderStyle BackColor=&quot;#AAAADD&quot; Font-Bold=&quot;True&quot; HorizontalAlign=&quot;Center&quot;&gt;&lt;/HeaderStyle&gt; &lt;PagerStyle HorizontalAlign=&quot;Right&quot; Mode=&quot;NumericPages&quot;&gt;&lt;/PagerStyle&gt; &lt;Columns&gt; &lt;asp:BoundColumn HeaderText=&quot;标题&quot; DataField=&quot;Title&quot; HeaderStyle-Width=&quot;480px&quot;&gt;&lt;/asp:BoundColumn&gt; &lt;asp:BoundColumn HeaderText=&quot;发表日期&quot; DataField=&quot;CreateDate&quot; DataFormatString=&quot;{0:yyyy-MM-dd hh:mm:ss}&quot;&gt;&lt;/asp:BoundColumn&gt; &lt;/Columns&gt; &lt;/asp:datagrid&gt; &lt;p align=&quot;center&quot;&gt; &lt;asp:label id=&quot;lblPageCount&quot; runat=&quot;server&quot; Font-Size=&quot;9pt&quot; Font-Bold=&quot;True&quot;&gt;&lt;/asp:label&gt;&amp;nbsp; &lt;asp:label id=&quot;lblCurrentIndex&quot; runat=&quot;server&quot; Font-Size=&quot;9pt&quot; Font-Bold=&quot;True&quot;&gt;&lt;/asp:label&gt; &lt;asp:linkbutton id=&quot;btnFirst&quot; runat=&quot;server&quot; Font-size=&quot;9pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;0&quot;/&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnPrev&quot; runat=&quot;server&quot; Font-size=&quot;9pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;prev&quot;/&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnNext&quot; runat=&quot;server&quot; Font-size=&quot;9pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;next&quot;/&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnLast&quot; runat=&quot;server&quot; Font-size=&quot;9pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;last&quot;/&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/HTML&gt;

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 { /// &lt;summary&gt; /// DataGridPaging 的摘要说明。 /// &lt;/summary&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 = &quot;最首页&quot;; btnPrev.Text = &quot;前一页&quot;; btnNext.Text = &quot;下一页&quot;; btnLast.Text = &quot;最后页&quot;; OpenDatabase(); BindGrid(); } private void OpenDatabase() { cn.ConnectionString = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; + HttpContext.Current.Server.MapPath(&quot;../../aspxWeb.mdb.ascx&quot;); cn.Open(); } private void ShowStats() { lblCurrentIndex.Text = &quot;第 &quot; + (MyDataGrid.CurrentPageIndex + 1).ToString() + &quot; 页&quot;; lblPageCount.Text = &quot;总共 &quot; + MyDataGrid.PageCount.ToString() + &quot; 页&quot;; } public void PagerButtonClick(object sender, EventArgs e) { string arg = ((LinkButton)sender).CommandArgument.ToString(); switch(arg) { case &quot;next&quot;: if (MyDataGrid.CurrentPageIndex &lt; (MyDataGrid.PageCount - 1)) { MyDataGrid.CurrentPageIndex += 1; } break; case &quot;prev&quot;: if (MyDataGrid.CurrentPageIndex &gt; 0) { MyDataGrid.CurrentPageIndex -= 1; } break; case &quot;last&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(&quot;Select Title,CreateDate from Document&quot;, myConnection); adapter.Fill(ds, &quot;Document&quot;); MyDataGrid.DataSource = ds.Tables[&quot;Document&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); } /// &lt;summary&gt; /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// &lt;/summary&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(); } } }

原文转自:http://www.ltesting.net