前人栽樹,後人乘涼,話不多說,代碼如下:
實現方式一:
.aspx:
<form id="form1" runat="server"> <table style="width: 605px"> <tr> <td style="width: 921px"> <asp:GridView ID="GridView1" runat="server" Width="700px" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" Height="232px"> <PagerSettings Visible="False" /> </asp:GridView> </td> </tr> <tr> <td style="width: 921px; height: 23px" >共 <asp:Label ID="recordCount" runat="server"></asp:Label> 條/ <asp:Label ID="LabelCurrentPage" runat="server"></asp:Label> <asp:Label ID="LabelPageCount" runat="server"></asp:Label>頁 <asp:LinkButton ID="First" runat="server" CommandArgument="First" CommandName="Page" OnClick="PagerButton_Click">首頁</asp:LinkButton> <asp:LinkButton ID="Prev" runat="server" CommandArgument="Prev" CommandName="Page" OnClick="PagerButton_Click">上一頁</asp:LinkButton> <asp:LinkButton ID="Next" runat="server" CommandArgument="Next" CommandName="Page" OnClick="PagerButton_Click">下一頁</asp:LinkButton> <asp:LinkButton ID="Last" runat="server" CommandArgument="Last" CommandName="Page" OnClick="PagerButton_Click">尾頁</asp:LinkButton> <asp:Label ID="Label1" runat="server">第</asp:Label> <asp:TextBox ID="txtPage" runat="server" Width="18px"></asp:TextBox> <asp:Label ID="Label2" runat="server">頁</asp:Label> <asp:Button ID="btnLoginPage" runat="server" Text="GO" OnClick="btnLoginPage_Click" /> </td> </tr> </table> </form>
.CS:(以下代碼可能摺疊,請自行點擊下面的按鈕展開)
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { //綁定資料 Bind(); } //計算產生分頁頁碼 this.First.CommandName = "1"; this.Prev.CommandName = this.GridView1.PageIndex == 0 ? "1" : this.GridView1.PageIndex.ToString(); if (this.GridView1.PageCount == 1) { this.Next.CommandName = this.GridView1.PageCount.ToString(); } else { int temp = this.GridView1.PageIndex + 2; this.Next.CommandName = temp.ToString(); } this.Last.CommandName = this.GridView1.PageCount.ToString(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; this.Bind(); } //點擊LinkButton按鈕事件 protected void PagerButton_Click(object sender, EventArgs e) { this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1; this.Bind(); } protected void Bind() { //資料繫結代碼 string strconn = "Data Source=.; Initial Catalog=JWInfo; uid=sa;pwd=123456;"; SqlConnection conn = new SqlConnection(strconn); conn.Open(); SqlCommand cmd = new SqlCommand("select * from 學生資訊", conn); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds, "學生資訊"); GridView1.DataSource = ds.Tables["學生資訊"]; GridView1.DataBind(); //總頁數,當前頁,總記錄數 this.recordCount.Text = ds.Tables[0].Rows.Count.ToString(); int t = this.GridView1.PageIndex+1; this.LabelCurrentPage.Text = t.ToString(); this.LabelPageCount.Text = this.GridView1.PageCount.ToString(); //設定相關按鈕是否可見 this.First.Enabled = Convert.ToBoolean(GridView1.PageIndex != 0); this.Prev.Enabled = Convert.ToBoolean(GridView1.PageIndex != 0); this.Next.Enabled = Convert.ToBoolean(this.GridView1.PageIndex != this.GridView1.PageCount - 1); this.Last.Enabled = Convert.ToBoolean(this.GridView1.PageIndex != this.GridView1.PageCount - 1); } //頁碼跳轉實現 protected void btnLoginPage_Click(object sender, EventArgs e) { int page = Convert.ToInt32(txtPage.Text.Trim()); if (page < 0 || page > GridView1.PageCount) { Response.Write("<mce:script language='javascript'><!-- alert('輸入的頁碼有錯請重新輸入'); // --></mce:script>"); } else { GridView1.PageIndex = page - 1; this.Bind(); } } }
實現方式二:
這裡通過使用GridView內建的分頁模板實現,代碼如下:
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" Width="740px" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"> <PagerTemplate> <asp:Label ID="Label1" runat="server" Text="第"></asp:Label> <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> <asp:Label ID="Label2" runat="server" Text="頁"></asp:Label> <asp:Label ID="Label3" runat="server" Text="共"></asp:Label> <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> <asp:Label ID="Label4" runat="server" Text="頁"></asp:Label> <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首頁</asp:LinkButton> <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一頁</asp:LinkButton> <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一頁</asp:LinkButton> <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾頁</asp:LinkButton> </PagerTemplate> </asp:GridView> </div> </form>
這其中,我曾看到有人,用類似的方法實現,但我在以下的代碼中實現了一點小變動,將其中的Visable屬性,改用Enable屬性,本人認為更為合理,並且有利布局:
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首頁</asp:LinkButton> <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一頁</asp:LinkButton> <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一頁</asp:LinkButton> <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾頁</asp:LinkButton>
.CS:
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { Bind(); } } protected void Bind() { string strconn = "Data Source=.; Initial Catalog=JWInfo; uid=sa;pwd=123456;"; SqlConnection conn = new SqlConnection(strconn); conn.Open(); SqlCommand cmd = new SqlCommand("select * from 學生資訊", conn); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds, "學生資訊"); GridView1.DataSource = ds.Tables["學生資訊"]; GridView1.DataBind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; this.Bind(); } }
以上是對別人實現的一些總結,和對其中一些代碼實現的改進,希望各位指教。