none
GridView 高效翻页问题? RRS feed

  • 问题

  • 依靠GridView本身自带的翻页功能似乎效率很低,尤其是数据量很大的时候。

    请问大家通常用什么方式进行翻页操作?或者介绍一些其他更好用的控件。

    谢谢。

    2010年4月18日 5:36

答案

  • 可以试试 repeater+AspNetPager,通过存储过程进行分页

    2010年4月18日 10:50
  • 分页主要看你的sql怎么写。如果不用存储过程,请参考

    select top 每页显示的记录数 * from topic where id not in  (select top (当前的页数-1)×每页显示的记录数 id from topic order by id desc)    order by id desc  

    也就是说如果你每页显示10条记录。这查询执行每次只会去数据库查询相应索引的10条数据。如果还想高效那就用存储过程高效分页。Bing 搜索很多

    以下是一个我常用的数字分页例子可参考

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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;
    using System.Text;
    public partial class Pager : System.Web.UI.Page
    {
      public int cpage;
      
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          if (Request.QueryString["page"] != null)
            cpage = Convert.ToInt32(Request.QueryString["page"]);
          else
            cpage = 1;
          RepeaterPager();
        }
      }
      private void RepeaterPager()
      {
        PagedDataSource ps = new PagedDataSource();
        DataTable dt = getPagesData(10,cpage);
        ps.DataSource = dt.DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 10;
        int total = Convert.ToInt32(getTotal());
        int maxpage = 1;
        if (total % ps.PageSize == 0)
        {
          maxpage = total / ps.PageSize;
        }
        else
        {
          maxpage = total / ps.PageSize + 1;
        }
        if (maxpage == 0) 
        { maxpage = 1; }
        if (cpage < 1)
        {
          Response.Redirect("Pager.aspx?page=1");
        }
        else if (cpage > maxpage)
        {
          Response.Redirect("Pager.aspx?page=" + maxpage + "");
        }
        ps.CurrentPageIndex = 0;
        Repeater1.DataSource = ps;
        Repeater1.DataBind();
        div_page.InnerHtml = PagerNum(cpage, maxpage, "Pager.aspx?page=");
      }
      /// <summary> 
      /// 取得分页总数 
      /// </summary> 
      /// <returns></returns> 
      public string getTotal()
      {
        StringBuilder sb = new StringBuilder();
        sb.Append("select count(*) from content");
        DataTable dt =DB.GetDt(sb.ToString());
        return dt.Rows[0][0].ToString();
      } 
    
      /// <summary> 
      /// 根据当前页码,每页条数,取得相应数据。 
      /// </summary> 
      /// <param name="pageNum">每页显示条数</param> 
      /// <param name="currentPage">当前页码</param> 
      /// <returns></returns> 
      public DataTable getPagesData(int dpageNum, int currentPage)
      {
        StringBuilder stb = new StringBuilder();
        if (currentPage == 1)
        {
          //如果是第一页就查询前10条
          stb.Append("select top " + dpageNum + " * from content order by id asc ");
        }
        else
        {
          stb.Append("select top " + dpageNum + " * from content where ");
          stb.Append("ID not in (select top " + dpageNum * (currentPage-1) + " ID from content order by id asc) order by id asc");
        }
        return DB.GetDt(stb.ToString());
      }
      /// <summary>
      /// 分页显示数字,才用字符累加输出
      /// </summary>
      /// <param name="pageNum">当前页数</param>
      /// <param name="pageCount">总页数</param>
      /// <param name="linkUrl">当前分页连接页面</param>
      /// <returns></returns>
      public string PagerNum(int pageNum, int pageCount, string linkUrl)
      {
        int startPage;
        StringBuilder pageControl = new StringBuilder();
        if (pageNum == 0)
        {
          pageNum = 1;     
        }
        int nextPage = pageNum + 1;
        int prevPage = pageNum - 1;
        pageControl.Append("<span>第"+pageNum+"页/共"+pageCount+"</span>");
        if (prevPage < 1)
        {
          pageControl.Append("<span>首页</span>");
          pageControl.Append("<span>前一页</span>");
        }
        else
        {
          pageControl.Append("<a href='" + linkUrl + 1 + "'>首页</a>");
          pageControl.Append("<a href='" + linkUrl + prevPage + "'>前一页</a>");
        }
        if (pageNum % 10 == 0)
        {
          startPage = pageNum - 9;
        }
        else
        {
          startPage = pageNum - pageNum % 10 + 1;
        }
        if (startPage > 10)
        {
          pageControl.Append("<span title='前十页'><a href='" + linkUrl + (startPage - 1) + "'>... ...</a></span>");
        }
        for (int i = startPage; i < startPage + 10; i++)
        {
          if (i > pageCount) break;
          if (i == pageNum)
          {
            pageControl.Append("<span title=第" + i + "条>" + i + "</span>"); 
          }
          else
          {
            pageControl.Append("<span title=第"+i+"><a href='"+linkUrl+i+"'>"+i+"</a><span>");
          }
        }
        if (pageCount >= startPage + 10)
        {
          pageControl.Append("<span title='后10页'><a href='"+linkUrl+(startPage+10)+"'>... ...</a></span>");
        }
        if (nextPage > pageCount)
        {
          pageControl.Append("<span title='下一页'>下一页</span>");
          pageControl.Append("<span title='尾页'>尾页</span>");
        }
        else
        {
          pageControl.Append("<span title='下一页'><a href='" + linkUrl + nextPage + "'>下一页</a></span>");
          pageControl.Append("<span title='尾页'><a href='"+linkUrl+pageCount+"'>尾页</a></span>");
        }
        return pageControl.ToString();
      }
    }
    

     


    啥叫乐观-得瑟的最高境界
    2010年4月18日 14:39

全部回复

  • 可以试试 repeater+AspNetPager,通过存储过程进行分页

    2010年4月18日 10:50
  • 分页主要看你的sql怎么写。如果不用存储过程,请参考

    select top 每页显示的记录数 * from topic where id not in  (select top (当前的页数-1)×每页显示的记录数 id from topic order by id desc)    order by id desc  

    也就是说如果你每页显示10条记录。这查询执行每次只会去数据库查询相应索引的10条数据。如果还想高效那就用存储过程高效分页。Bing 搜索很多

    以下是一个我常用的数字分页例子可参考

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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;
    using System.Text;
    public partial class Pager : System.Web.UI.Page
    {
      public int cpage;
      
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          if (Request.QueryString["page"] != null)
            cpage = Convert.ToInt32(Request.QueryString["page"]);
          else
            cpage = 1;
          RepeaterPager();
        }
      }
      private void RepeaterPager()
      {
        PagedDataSource ps = new PagedDataSource();
        DataTable dt = getPagesData(10,cpage);
        ps.DataSource = dt.DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 10;
        int total = Convert.ToInt32(getTotal());
        int maxpage = 1;
        if (total % ps.PageSize == 0)
        {
          maxpage = total / ps.PageSize;
        }
        else
        {
          maxpage = total / ps.PageSize + 1;
        }
        if (maxpage == 0) 
        { maxpage = 1; }
        if (cpage < 1)
        {
          Response.Redirect("Pager.aspx?page=1");
        }
        else if (cpage > maxpage)
        {
          Response.Redirect("Pager.aspx?page=" + maxpage + "");
        }
        ps.CurrentPageIndex = 0;
        Repeater1.DataSource = ps;
        Repeater1.DataBind();
        div_page.InnerHtml = PagerNum(cpage, maxpage, "Pager.aspx?page=");
      }
      /// <summary> 
      /// 取得分页总数 
      /// </summary> 
      /// <returns></returns> 
      public string getTotal()
      {
        StringBuilder sb = new StringBuilder();
        sb.Append("select count(*) from content");
        DataTable dt =DB.GetDt(sb.ToString());
        return dt.Rows[0][0].ToString();
      } 
    
      /// <summary> 
      /// 根据当前页码,每页条数,取得相应数据。 
      /// </summary> 
      /// <param name="pageNum">每页显示条数</param> 
      /// <param name="currentPage">当前页码</param> 
      /// <returns></returns> 
      public DataTable getPagesData(int dpageNum, int currentPage)
      {
        StringBuilder stb = new StringBuilder();
        if (currentPage == 1)
        {
          //如果是第一页就查询前10条
          stb.Append("select top " + dpageNum + " * from content order by id asc ");
        }
        else
        {
          stb.Append("select top " + dpageNum + " * from content where ");
          stb.Append("ID not in (select top " + dpageNum * (currentPage-1) + " ID from content order by id asc) order by id asc");
        }
        return DB.GetDt(stb.ToString());
      }
      /// <summary>
      /// 分页显示数字,才用字符累加输出
      /// </summary>
      /// <param name="pageNum">当前页数</param>
      /// <param name="pageCount">总页数</param>
      /// <param name="linkUrl">当前分页连接页面</param>
      /// <returns></returns>
      public string PagerNum(int pageNum, int pageCount, string linkUrl)
      {
        int startPage;
        StringBuilder pageControl = new StringBuilder();
        if (pageNum == 0)
        {
          pageNum = 1;     
        }
        int nextPage = pageNum + 1;
        int prevPage = pageNum - 1;
        pageControl.Append("<span>第"+pageNum+"页/共"+pageCount+"</span>");
        if (prevPage < 1)
        {
          pageControl.Append("<span>首页</span>");
          pageControl.Append("<span>前一页</span>");
        }
        else
        {
          pageControl.Append("<a href='" + linkUrl + 1 + "'>首页</a>");
          pageControl.Append("<a href='" + linkUrl + prevPage + "'>前一页</a>");
        }
        if (pageNum % 10 == 0)
        {
          startPage = pageNum - 9;
        }
        else
        {
          startPage = pageNum - pageNum % 10 + 1;
        }
        if (startPage > 10)
        {
          pageControl.Append("<span title='前十页'><a href='" + linkUrl + (startPage - 1) + "'>... ...</a></span>");
        }
        for (int i = startPage; i < startPage + 10; i++)
        {
          if (i > pageCount) break;
          if (i == pageNum)
          {
            pageControl.Append("<span title=第" + i + "条>" + i + "</span>"); 
          }
          else
          {
            pageControl.Append("<span title=第"+i+"><a href='"+linkUrl+i+"'>"+i+"</a><span>");
          }
        }
        if (pageCount >= startPage + 10)
        {
          pageControl.Append("<span title='后10页'><a href='"+linkUrl+(startPage+10)+"'>... ...</a></span>");
        }
        if (nextPage > pageCount)
        {
          pageControl.Append("<span title='下一页'>下一页</span>");
          pageControl.Append("<span title='尾页'>尾页</span>");
        }
        else
        {
          pageControl.Append("<span title='下一页'><a href='" + linkUrl + nextPage + "'>下一页</a></span>");
          pageControl.Append("<span title='尾页'><a href='"+linkUrl+pageCount+"'>尾页</a></span>");
        }
        return pageControl.ToString();
      }
    }
    

     


    啥叫乐观-得瑟的最高境界
    2010年4月18日 14:39