天天看點

DataList裡面用C#實作自定義分頁

PagedDataSource pds=new PagedDataSource();

          //設定分頁對象的資料源

           pds.DataSource=dataSet11.Tables["leaveWords"].DefaultView;

          //啟用分頁功能

          pds.AllowPaging=true;

          //每頁4行

          pds.PageSize=4;

          //目前頁号

          int CurrentPageNo;

          //頁面是否跳轉

          if(Request.QueryString["Page"]!=null)

             //若有跳轉請求,将目前頁号設定到請求的頁号

             CurrentPageNo=Convert.ToInt32(Request.QueryString["Page"]);

          else

             //否則目前頁号為1

             CurrentPageNo=1;

          //設定分頁對象的目前頁的索引

          pds.CurrentPageIndex=CurrentPageNo-1;

          LabelCount.Text="共"+pds.PageCount.ToString()+"頁";

          LabelCurrent.Text="第"+CurrentPageNo.ToString()+"頁";

          //設定"上一頁"和"下一頁"的導航路徑

          if(!pds.IsFirstPage)

             HyperLinkUp.NavigateUrl=Request.CurrentExecutionFilePath+

               "?Page="+Convert.ToString(CurrentPageNo-1);

          if(!pds.IsLastPage)

             HyperLinkDown.NavigateUrl=Request.CurrentExecutionFilePath+

               "?Page="+Convert.ToString(CurrentPageNo+1);

          //資料綁定到控件DataList1

          DataList1.DataSource=pds;

          DataList1.DataBind();