• GridView自定义分页,模仿GridView的自动分页功能


    这个分页是自定义分页,只从数据库中读取当前的数据,样子看着跟GridView的自动分页基本上完全一样。

    1、用两个ViewState保存当前页数CurrentPage和总行数RowCount

    #region 分页
            /// <summary>
            /// 当前页
            /// </summary>
            private int CurrentPage
            {
                get
                {
                    if (ViewState["CurrentPage"] == null)
                    {
                        return 0; // 页数从0开始
                    }
                    else
                    {
                        return (int)ViewState["CurrentPage"];
                    }
                }
                set
                {
                    ViewState["CurrentPage"] = value;
                }
            }

            /// <summary>
            /// 总行数
            /// </summary>
            private int RowCount
            {
                get
                {
                    if (ViewState["RowCount"] == null)
                    {
                        return 0;
                    }
                    else
                    {
                        return (int)ViewState["RowCount"];
                    }
                }
                set
                {
                    ViewState["RowCount"] = value;
                }
            }
            #endregion

    2、加载数据 

    private void LoadData()
            {
                。。。。。。

                int totalCount = bll.GetRowcount(letter, teamName);// 总行数
                RowCount = totalCount;// 保存总行数到ViewState中

         // 得到当前页的数据,前两个参数分别是每页显示的记录数和当前是第几页

                List<StdTeamInfo> lstStdTeam = bll.GetListByPager(GridView1.PageSize, CurrentPage + 1, letter, teamName);
                GridView1.DataSource = lstStdTeam;
                GridView1.DataBind();
            }

    3、加GridView的分页方法,跟自动分页时加分页方法一样

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                this.CurrentPage = e.NewPageIndex;//保存当前是第几页到ViewState中
                LoadData();
            }

    4、重写OnPreRender,加分页的链接 

    protected override void OnPreRender(EventArgs e)
            {

        // Utility.GetPagerString,此方法在第5步介绍,功能是得到分页的链接字符串
                string pagerString = Utility.GetPagerString(RowCount, (CurrentPage + 1), GridView1.PageSize, this.GridView1.ClientID);
                if (pagerString != "")
                {
                    int index = this.GridView1.Rows.Count;
                    GridViewRow rowHeader = new GridViewRow(index, index, DataControlRowType.Pager, DataControlRowState.Normal);
                    TableCell headerCell = new TableCell();
                    headerCell.ColumnSpan = 8;
                    headerCell.Text = pagerString;
                    rowHeader.Cells.Add(headerCell);
                    this.GridView1.Controls[0].Controls.Add(rowHeader);
                }
            }

    5、GetPagerString方法,得到分页的链接字符串

    /// <summary>
            /// 返回分页的链接串
            /// </summary>
            /// <param name="totalCount">总行数</param>
            /// <param name="currentPage">当前第几页(从0开始)</param>
            /// <param name="pageSize">每页记录数</param>
            /// <param name="gridViewID">GridView控件ID</param>
            /// <returns></returns>
            public static string GetPagerString(int totalCount, int currentPage, int pageSize,
                string gridViewID)
            {
                gridViewID = gridViewID.Replace("_", "$"); 
                // 总页数
                int pageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);
                if (pageCount == 1 || pageCount == 0) return "";

                int pageFrom = (currentPage - (currentPage % 30 == 0 ? 30 : currentPage % 30)) + 1; // 这里把显示的最大页面数设为了30
                if (pageFrom < 0) pageFrom = 1;
                int pageTo = currentPage + (30 - (currentPage % 30 == 0 ? 30 : currentPage % 30));
                if (pageTo > pageCount) pageTo = pageCount;

                StringBuilder sb = new StringBuilder();
                sb.Append("<table border=\"0\"><tr>");
                if (currentPage > 30)
                    sb.Append("<td><a href=\"javascript:__doPostBack('" + gridViewID + "','Page$" + (pageFrom - 1) + "')\">...</a></td>");
                for (int i = pageFrom; i <= pageTo; i++)
                {
                    if (i == currentPage)
                        sb.Append("<td><span>" + i + "</span></td>");
                    else
                        sb.Append("<td><a href=\"javascript:__doPostBack('" + gridViewID + "','Page$" + i + "')\">" + i + "</a></td>");
                }
                if (pageTo < pageCount)
                    sb.Append("<td><a href=\"javascript:__doPostBack('" + gridViewID + "','Page$" + (pageTo + 1) + "')\">...</a></td>");

                sb.Append("<td>共" + pageCount + "页</td>");

                sb.Append("</tr></table>");

                return sb.ToString();
            }

    至此这个自定义分页完成了。样式跟GridView的自定义分页一样,但是只读出需要的数据,页面显示速度快了不少。

  • 相关阅读:
    mysql5.7密码修改与报错分析
    Nginx安装与升级(包括虚拟主机)
    寻找【System.FormatException: Base64 字符数组的无效长度】的解决方法
    关于sql server 2008 R2中遇到的sql server 代理启动不了的解决过程
    【EDM经验交流】之如何监控某个域名邮箱发送到网易的邮件接收情况?
    microsoft sql server 2008 5123
    ms sql server 2008 数据库自动备份作业权限问题
    【EDM经验交流】之邮件服务器容量判断
    nextSibling
    一步一步理解拖拽Drag(一)
  • 原文地址:https://www.cnblogs.com/ggooo/p/1651403.html
Copyright © 2020-2023  润新知