AspNetPager+GridView+SqlDataSource实现分页效果

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值