(1)去官网下载最新AspNetPager
(2)右键你的项目—>添加引用,把刚才下载的AspNetPager里的AspNetPager.dll添加到你项目的bin文件夹里
(3)在前台页面加入<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>,后台页面加入using Wuqi.Webdiyer;(4)接着在GridView中添加以下代码(页导航行),记得把GridView的AllowPaging="True":
<PagerTemplate>
<webdiyer:AspNetPager CssClass="pages" CurrentPageButtonClass="cpb" ID="AspNetPager1" runat="server" PageSize="<%# ((GridView)Container.NamingContainer).PageSize%>" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" OnPageChanging="AspNetPager1_PageChanging"></webdiyer:AspNetPager>
</PagerTemplate>
(5)后台页面:
public partial class Animation : System.Web.UI.Page
{
int recordcount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();//绑定代码
}
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex - 1;//这儿需要注意,AspNetPager中的PageIndex是从1开始的,而GridView的是从0开始的,所以要减1.这里是令GridView1跳到当前页面
bind();//GridView的数据绑定方法
AspNetPager pager = this.GridView1.BottomPagerRow.FindControl("AspNetPager1") as AspNetPager;
pager.CurrentPageIndex = e.NewPageIndex; //这里是告诉AspNetPager当前页面是多少
}
protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
recordcount = Convert.ToInt32(e.AffectedRows); //e.AffectedRows获取的就是总记录数
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow row = GridView1.BottomPagerRow; //获取GridView的PagerTemplate,即分页导航行
if (row != null) ((AspNetPager)(row.FindControl("AspNetPager1"))).RecordCount = recordcount;
}
public void bind()
{
SqlDataSource1.SelectCommand = "SELECT语句";
SqlDataSource1.DataBind();
GridView1.DataBind();
}
}