GridView控件自定义分页的实现



前人栽树,后人乘凉,话不多说,代码如下:



 



 

实现方式一:

.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();
}

}


 

以上是对别人实现的一些总结,和对其中一些代码实现的改进,希望各位指教!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



版权声明:本文为博主原创文章,未经博主允许不得转载。