友情链接:.NET项目开发者博客

实现分页的两种方法

分页的两种方法:

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

    客户端:

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

 

posted @ 2013-03-20 21:16  千年老妖  Views(605)  Comments(0Edit  收藏  举报
友情链接:.NET项目开发者博客