zoukankan      html  css  js  c++  java
  • 实现分页的两种方法

    分页的两种方法:

    第一种:如果利用的是动软代码生成器生成的代码:

        客户端:

              <div>

               <%for (int i = 1; i <=PageCount; i++)

                 {%>

               <a href="ShowNewsList.aspx?pageIndex=<%= i %>">

               

                    <%= i %>  

               </a>

               <%%>

               </div>

             服务器端:

             BLL.HKSJ_Main bllmain = new BLL.HKSJ_Main();

               //将当前请求的页的数字传到服务器端

               int pageIndex=Request["pageIndex"]==null?1:int.Parse(Request["pageIndex"]);

               //将对应的请求的存在于该页的数据显示出来

               DataSet ds = bllmain.GetListByPage(string.Empty, "id", (pageIndex - 1) * 3      + 1, pageIndex * 3);

                list = bllmain.DataTableToList(ds.Tables[0]);

                //得到总页数

                PageCount = (bllmain.GetRecordCount(string.Empty) + 3 - 1) / 3;

    第二种:利用存储过程进行分页:

             例如:在数据库中进行存储过程:

              执行存储过程

              create proc PR_pageLoadData

              @pageIndex int,

              @pageSize int,

              @total int out

              as 

             select top(@pageSize) * from HKSJ_Main where ID not in

             (

                select top((@pageIndex-1)*@pageSize) ID from HKSJ_Main  order by ID

              )

            order by ID

           go

     

           declare @total int 

           exec PR_pageLoadData 4,5 ,@total out

          print @total

          go

     

     

     

    public List<Model.HKSJ_Main>  GetPage(int pageIndex, int pageSize, out int total)

          {

              SqlParameter totalPara=new SqlParameter("@total",SqlDbType.Int);

              DataSet ds=new DataSet();

              using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))

              {

                  conn.Open();

                  using (SqlDataAdapter adapter = new SqlDataAdapter("PR_pageLoadData", conn))

                  {

                      adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

     

                      adapter.SelectCommand.Parameters.AddWithValue("@pageSize", pageSize);

                   adapter.SelectCommand.Parameters.AddWithValue("@pageIndex" pageIndex);

     

                      totalPara.Direction = ParameterDirection.Output;

                      adapter.SelectCommand.Parameters.Add(totalPara);

     

                      adapter.Fill(ds);

                  }

     

              }

              total = (int)totalPara.Value;

     

              return DataTableToList(ds.Tables[0]);

          }

         DataTableToList的方法完全可以copy动软代码生成器中的DataTableToList方法。

         然后在对应的aspx.cs页面中调用GetPage()方法

          int pageIndex=Request["pageIndex"]==null?1:int.Parse(Request["pageIndex"]);

          HKSJ_MainExt mianext = new HKSJ_MainExt();

          int total = 0;

          list = mianext.GetPage(pageIndex, 3, out total);    

     

  • 相关阅读:
    SLS评测报告
    Flash对不同的浏览器的兼容性
    NodeJS的Cluster模块使用
    Varnish+Xcache构建高性能WEB构架初探
    Memcached Client的释疑
    Firebug及YSlow简介与使用图文详解
    PHP Memcached 实现简单数据库缓存
    PHP + Memcache 实现Session共享
    Linux 开机关机在线求助与指令输入
    Linux 基础学习篇笔记 Linux基础知识
  • 原文地址:https://www.cnblogs.com/hanwenhuazuibang/p/2971936.html
Copyright © 2011-2022 走看看