AspNetPager 与PagedDataSource 配合为GRIDVIEW控件分页

来源:互联网 发布:免费刷svip永久软件 编辑:程序博客网 时间:2024/06/05 16:57

原理:

        1、不用反复读库,不用多次绑定

        2、绑定一次,将结果存在页面缓存




首先在页面引入:<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

页面HTML代码

<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" CssClass="pages"             CurrentPageButtonClass="currentpage" CustomInfoHTML="共%RecordCount%条,当前为第%CurrentPageIndex%页,共%PageCount%页"            CustomInfoSectionWidth="40%" CustomInfoTextAlign="Left" HorizontalAlign="Right"             NumericButtonCount="10"            OnPageChanged="AspNetPager1_PageChanged" ShowBoxThreshold="10" ShowCustomInfoSection="Left" Visible="true"            ShowFirstLast="false" ShowPageIndexBox="Never" Width="100%" PageSize="15">            </webdiyer:AspNetPager>

后台:

1、定义页面变量

 public DataTable PageTable
        {
            get
            {
                return ViewState["PageTable"] != null ? (DataTable)ViewState["PageTable"] : null;
            }
            set
            {
                ViewState["PageTable"] = value;
            }
        }

2、将从数据库中读取的DataTable赋值给PageTable

即 PageTable=dt(从数据库读取的结果)

3.调用数据绑定方法 GridViewDataBound()

private void GridViewDataBound()
        {
            //初始化分页数据源实例
            PagedDataSource pds = new PagedDataSource();
            //设置总行数
            AspNetPager1.RecordCount = PageTable.Rows.Count;
            //设置分页的数据源
            pds.DataSource = PageTable.DefaultView;
            //设置当前页
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;   //AspNetPager1是AspNetPager的ID
            //设置每页显示页数
            pds.PageSize = AspNetPager1.PageSize;
            //启用分页
            pds.AllowPaging = true;
            //设置GridView的数据源为分页数据源
            GV_TJData.DataSource = pds;  //GV_TJData是绑定数据的GridView的ID
            //绑定GridView
            GV_TJData.DataBind();
        }



protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
          
            GridViewDataBound();//当页码改变时,重新绑定GridView即可,不需要重新读取数据库
        }



0 0
原创粉丝点击