一、前端準備工作

1、之前我寫到過《Asp.net中引用AspNetPager.dll進行資料分頁》  這種分頁方式只能在前臺將資料分頁,而每次點選查詢時對目標資料庫還是全查詢,這樣不僅會消耗資料庫資源,還會加長等待時間,所以本文我將介紹如何通過儲存過程對資料進行真分頁。

2、參考前文將基礎搭建完成。

二、分頁儲存過程。

1、在真分頁中,我們要將每一頁的資料量,起始行這些傳送給後端,後端接收到指令後則按照這個區間進行查詢資料,後端資料分頁方式在前文中我有詳細介紹過。

2、後端分頁:SQL Server的兩種資料分頁方式簡析 ,參考此文的分頁方式選擇合適自己後端分頁方法,後面我使用的是儲存過程分頁-GetPageRecords。

三、頁面程式碼

PS:在AspNetPager.dl開發者官網,也有其它呼叫方式和用法,大家可以看看有沒有新的思路,對於分頁控制元件樣式,網上有一些CSS樣式可以參考,建議看看,因為原格式實在有些單調。

  1. <webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="25"
  2. HorizontalAlign="Center" Width="100%"
  3. meta:resourceKey="AspNetPager1" Style="font-size: 14px"
  4. AlwaysShow="false" FirstPageText="首頁" LastPageText="尾頁" NextPageText="後頁"
  5. PrevPageText="前頁" SubmitButtonText="Go" SubmitButtonClass="submitBtn"
  6. CustomInfoStyle="font-size:14px;text-align:left;"
  7. InputBoxStyle="width:25px; border:1px solid #999999; text-align:center; "
  8. TextBeforeInputBox="轉到第" TextAfterInputBox="頁 " TextAfterPageIndexBox="頁"
  9. TextBeforePageIndexBox="轉到" Font-Size="14px" CustomInfoHTML="共有%RecordCount%條記錄,共%PageCount%頁,每頁%PageSize%行"
  10. ShowCustomInfoSection="Left" CustomInfoSectionWidth="30%"
  11. CssClass="paginator"
  12. PagingButtonSpacing="3px" OnPageChanged="AspNetPager1_PageChanged" ShowBoxThreshold="25">
  13. </webdiyer:AspNetPager

常用自定義項:PageSize:頁面顯示的行數;

CustomInfoHTML:自定義項(這裡可以任意新增刪除)

四、後臺程式碼

PS:本文中的程式碼是從我的測試專案中扣出來的,可根據自己的專案靈活應用,如果有更好的方式和思路歡迎留言。

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)//判斷是否回發
  4. {
  5. RowDatasBind();//呼叫函式
  6. }
  7. }
  8. //拼接頁面查詢條件
  9. public string Search()
  10. {
  11. string SQL = "select PCB_CODE,WORK_ORDER_CODE,PRODUCTION_ORDER_ID,TEST_CODE,PLANT_CODE,CREATE_TIME,DEVICE_NO,(case CONCLUSION when 0 then '通過' when 1 then '不通過' else '其他' end) CONCLUSION from [dbo].[T_Aoi_TEST] WITH (NOLOCK) where 1=1";
  12. if (!string.IsNullOrEmpty(txtWORK_ORDER_CODE.Text))
  13. {
  14. SQL = SQL + " and WORK_ORDER_CODE like'" + "%" + txtWORK_ORDER_CODE.Text.Trim() + "%" + "'";
  15. }
  16. if (!string.IsNullOrEmpty(txtPCB_CODE.Text))
  17. {
  18. SQL = SQL + " and PCB_CODE like'" + "%" + txtPCB_CODE.Text.Trim() + "%" + "'";
  19. }
  20. if (!string.IsNullOrEmpty(txtPRODUCTION_ORDER_ID.Text))
  21. {
  22. SQL = SQL + " and PRODUCTION_ORDER_ID like'" + "%" + txtPRODUCTION_ORDER_ID.Text.Trim() + "%" + "'";
  23. }
  24. if (!string.IsNullOrEmpty(txtBeginTime.Text) && !string.IsNullOrEmpty(txtEndTime.Text))
  25. {
  26. SQL = SQL + " and CHECK_TIME>='" + txtBeginTime.Text + "'" + " and CHECK_TIME<='" + txtEndTime.Text + "'";
  27. }
  28. if (ddlCon.Text != "9")
  29. {
  30. SQL = SQL + " and CONCLUSION ='" + ddlCon.Text + "'";
  31. }
  32. //按建立時間倒序查詢
  33. SQL = SQL + "order by CREATE_TIME desc";
  34. return SQL;
  35. }
  36.  
  37. //伺服器端資料分頁
  38. private void RowDatasBind()
  39. {
  40. string SearchConditions = "1=1";//搜尋條件
  41. string SQLgetcount = "select count(1) from T_Aoi_TEST where 1=1";
  42.  
  43. if (!string.IsNullOrEmpty(txtWORK_ORDER_CODE.Text))
  44. {
  45. SearchConditions = SearchConditions + " and WORK_ORDER_CODE like''" + "%" + txtWORK_ORDER_CODE.Text.Trim() + "%" + "''";
  46. SQLgetcount = SQLgetcount + " and WORK_ORDER_CODE like'" + "%" + txtWORK_ORDER_CODE.Text.Trim() + "%" + "'";
  47. }
  48. if (!string.IsNullOrEmpty(txtPCB_CODE.Text))
  49. {
  50. SearchConditions = SearchConditions + " and PCB_CODE like''" + "%" + txtPCB_CODE.Text.Trim() + "%" + "''";
  51. SQLgetcount = SQLgetcount + " and PCB_CODE like'" + "%" + txtPCB_CODE.Text.Trim() + "%" + "'";
  52. }
  53. if (!string.IsNullOrEmpty(txtPRODUCTION_ORDER_ID.Text))
  54. {
  55. SearchConditions = SearchConditions + " and PRODUCTION_ORDER_ID like''" + "%" + txtPRODUCTION_ORDER_ID.Text.Trim() + "%" + "''";
  56. SQLgetcount = SQLgetcount + " and PRODUCTION_ORDER_ID like'" + "%" + txtPRODUCTION_ORDER_ID.Text.Trim() + "%" + "'";
  57. }
  58. if (!string.IsNullOrEmpty(txtBeginTime.Text) && !string.IsNullOrEmpty(txtEndTime.Text))
  59. {
  60. SearchConditions = SearchConditions + " and CHECK_TIME>=''" + txtBeginTime.Text + "''" + " and CHECK_TIME<=''" + txtEndTime.Text + "''";
  61. SQLgetcount = SQLgetcount + " and CHECK_TIME>='" + txtBeginTime.Text + "'" + " and CHECK_TIME<='" + txtEndTime.Text + "'";
  62. }
  63. if (ddlCon.Text != "9")
  64. {
  65. SearchConditions = SearchConditions + " and CONCLUSION =''" + ddlCon.Text + "''";
  66. SQLgetcount = SQLgetcount + " and CONCLUSION ='" + ddlCon.Text + "'";
  67. }
  68.  
  69. int StartRow = (AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize) - AspNetPager1.PageSize;//計算起始行
  70. string SQLGetPage = "EXEC [Common_GetPageRecords] @StartRow=" + StartRow + ",@MaxRows = " + AspNetPager1.PageSize + ",@TableName = N'T_Aoi_TEST',@PrimaryKey = N'TEST_CODE',@GetFields = N'PCB_CODE,WORK_ORDER_CODE,PRODUCTION_ORDER_ID,TEST_CODE,PLANT_CODE,CREATE_TIME,DEVICE_NO,(case CONCLUSION when 0 then ''通過'' when 1 then ''不通過'' else ''其他'' end) CONCLUSION',@SearchConditions ='" + SearchConditions + "',@SortExpression = N'TEST_CODE desc'";
  71.  
  72. //計算總行數
  73. DataSet dss = DBHelper.GetDataSetMis(SQLgetcount);
  74. DataTable fileNameDt = dss.Tables[0];
  75. this.AspNetPager1.RecordCount = Convert.ToInt32(fileNameDt.Rows[0][0].ToString());//總行數
  76. //分頁後資料填充
  77. DataSet ds = DBHelper.GetDataSetMis(SQLGetPage);
  78. PagedDataSource pds = new PagedDataSource();
  79. pds.AllowPaging = true;//是否開啟分頁
  80. pds.AllowServerPaging = true;//是否開啟伺服器端分頁
  81. pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex;//當前頁的頁碼
  82. pds.PageSize = AspNetPager1.PageSize;//每頁顯示的行數
  83. int aaa = ds.DefaultViewManager.DataSet.Tables.Count;
  84. if (ds.DefaultViewManager.DataSet.Tables.Count == 0)//修復當查詢結果為空時“無法找到表 0。”的錯誤
  85. {
  86. pds.DataSource = null;
  87. }
  88. else
  89. {
  90. pds.DataSource = ds.Tables[0].DefaultView;
  91. if (ds.Tables[0].Rows.Count != AspNetPager1.PageSize)//修復最後一頁因為剩餘尾數和頁數不對應出現索引錯誤的問題
  92. {
  93. pds.PageSize = ds.Tables[0].Rows.Count;
  94. }
  95. }
  96. this.GridView1.DataSource = pds;
  97. this.GridView1.DataBind();
  98. }
  99. /// <summary>
  100. /// 繫結資料到分頁控制元件
  101. /// </summary>
  102. /// <param name="sender"></param>
  103. /// <param name="e"></param>
  104. protected void AspNetPager1_PageChanged(object sender, EventArgs e)
  105. {
  106. RowDatasBind();
  107. }
  108. /// <summary>
  109. /// 條件查詢
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. protected void btnQuery_Click(object sender, EventArgs e)
  114. {
  115. RowDatasBind();
  116. }

五、總結

到這裡真假分頁以及分頁儲存過程都已經完成了,頁面載入時再也不用漫長的等待。如果你有更好方法建議歡迎吐槽、留言~