GridView控件自定义分页的实现
2009-10-05 17:15
645 查看
前人栽树,后人乘凉,话不多说,代码如下:
实现方式一:
.aspx:
.CS:(以下代码可能折叠,请自行点击下面的按钮展开)
实现方式二:
这里通过使用GridView自带的分页模板实现,代码如下:
这其中,我曾看到有人,用类似的方法实现,但我在以下的代码中实现了一点小变动,将其中的Visable属性,改用Enable属性,本人认为更为合理,并且有利布局:
.CS:
以上是对别人实现的一些总结,和对其中一些代码实现的改进,希望各位指教!
实现方式一:
.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(); } }
以上是对别人实现的一些总结,和对其中一些代码实现的改进,希望各位指教!
相关文章推荐
- GridView控件自定义分页的实现
- 扩展GridView实现的一个自定义无刷新分页,排序,支持多种数据源的控件TwfGridView
- 对《自定义GridView分页模板中页码Button的实现》中程序的改进,追加显示空行功能
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除
- 用Gridview和AspNetPager控件实现真分页(一)
- GridView控件自定义分页详解
- GridView控件实现分页
- 控件也是类——分步实现具有分页功能的自定义DataList控件【附源代码】
- 使用AspNetPager控件实现GridView分页
- Android GridView控件分页自定义
- GridView控件实现自定义数字、时间、货币字符串格式
- VS-利用GridView控件实现假分页
- 使用Gridview和ObjectDataSource轻松实现自定义分页
- 给AspNetPager控件和GridView的分页添加自定义样式
- 自定义GridView分页模板中页码Button的实现(上)
- 安卓_手机卫士_第二天(GridView,自定义组合控件实现设置页项,自定义对话框)
- ASP.NET技巧:GridView控件自定义分页详解
- GridView分页的实现以及自定义分页样式功能实例
- 使用Gridview和ObjectDataSource轻松实现自定义分页
- GridView控件实现分页功能