一、前端準備工作
1、之前我寫到過《Asp.net中引用AspNetPager.dll進行資料分頁》 這種分頁方式只能在前臺將資料分頁,而每次點選查詢時對目標資料庫還是全查詢,這樣不僅會消耗資料庫資源,還會加長等待時間,所以本文我將介紹如何通過儲存過程對資料進行真分頁。
2、參考前文將基礎搭建完成。
二、分頁儲存過程。
1、在真分頁中,我們要將每一頁的資料量,起始行這些傳送給後端,後端接收到指令後則按照這個區間進行查詢資料,後端資料分頁方式在前文中我有詳細介紹過。
2、後端分頁:SQL Server的兩種資料分頁方式簡析 ,參考此文的分頁方式選擇合適自己後端分頁方法,後面我使用的是儲存過程分頁-GetPageRecords。
三、頁面程式碼
PS:在AspNetPager.dl開發者官網,也有其它呼叫方式和用法,大家可以看看有沒有新的思路,對於分頁控制元件樣式,網上有一些CSS樣式可以參考,建議看看,因為原格式實在有些單調。
- <webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="25"
- HorizontalAlign="Center" Width="100%"
- meta:resourceKey="AspNetPager1" Style="font-size: 14px"
- AlwaysShow="false" FirstPageText="首頁" LastPageText="尾頁" NextPageText="後頁"
- PrevPageText="前頁" SubmitButtonText="Go" SubmitButtonClass="submitBtn"
- CustomInfoStyle="font-size:14px;text-align:left;"
- InputBoxStyle="width:25px; border:1px solid #999999; text-align:center; "
- TextBeforeInputBox="轉到第" TextAfterInputBox="頁 " TextAfterPageIndexBox="頁"
- TextBeforePageIndexBox="轉到" Font-Size="14px" CustomInfoHTML="共有%RecordCount%條記錄,共%PageCount%頁,每頁%PageSize%行"
- ShowCustomInfoSection="Left" CustomInfoSectionWidth="30%"
- CssClass="paginator"
- PagingButtonSpacing="3px" OnPageChanged="AspNetPager1_PageChanged" ShowBoxThreshold="25">
- </webdiyer:AspNetPager
常用自定義項:PageSize:頁面顯示的行數;
CustomInfoHTML:自定義項(這裡可以任意新增刪除)
四、後臺程式碼
PS:本文中的程式碼是從我的測試專案中扣出來的,可根據自己的專案靈活應用,如果有更好的方式和思路歡迎留言。
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)//判斷是否回發
- {
- RowDatasBind();//呼叫函式
- }
- }
- //拼接頁面查詢條件
- public string Search()
- {
- 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";
- if (!string.IsNullOrEmpty(txtWORK_ORDER_CODE.Text))
- {
- SQL = SQL + " and WORK_ORDER_CODE like'" + "%" + txtWORK_ORDER_CODE.Text.Trim() + "%" + "'";
- }
- if (!string.IsNullOrEmpty(txtPCB_CODE.Text))
- {
- SQL = SQL + " and PCB_CODE like'" + "%" + txtPCB_CODE.Text.Trim() + "%" + "'";
- }
- if (!string.IsNullOrEmpty(txtPRODUCTION_ORDER_ID.Text))
- {
- SQL = SQL + " and PRODUCTION_ORDER_ID like'" + "%" + txtPRODUCTION_ORDER_ID.Text.Trim() + "%" + "'";
- }
- if (!string.IsNullOrEmpty(txtBeginTime.Text) && !string.IsNullOrEmpty(txtEndTime.Text))
- {
- SQL = SQL + " and CHECK_TIME>='" + txtBeginTime.Text + "'" + " and CHECK_TIME<='" + txtEndTime.Text + "'";
- }
- if (ddlCon.Text != "9")
- {
- SQL = SQL + " and CONCLUSION ='" + ddlCon.Text + "'";
- }
- //按建立時間倒序查詢
- SQL = SQL + "order by CREATE_TIME desc";
- return SQL;
- }
- //伺服器端資料分頁
- private void RowDatasBind()
- {
- string SearchConditions = "1=1";//搜尋條件
- string SQLgetcount = "select count(1) from T_Aoi_TEST where 1=1";
- if (!string.IsNullOrEmpty(txtWORK_ORDER_CODE.Text))
- {
- SearchConditions = SearchConditions + " and WORK_ORDER_CODE like''" + "%" + txtWORK_ORDER_CODE.Text.Trim() + "%" + "''";
- SQLgetcount = SQLgetcount + " and WORK_ORDER_CODE like'" + "%" + txtWORK_ORDER_CODE.Text.Trim() + "%" + "'";
- }
- if (!string.IsNullOrEmpty(txtPCB_CODE.Text))
- {
- SearchConditions = SearchConditions + " and PCB_CODE like''" + "%" + txtPCB_CODE.Text.Trim() + "%" + "''";
- SQLgetcount = SQLgetcount + " and PCB_CODE like'" + "%" + txtPCB_CODE.Text.Trim() + "%" + "'";
- }
- if (!string.IsNullOrEmpty(txtPRODUCTION_ORDER_ID.Text))
- {
- SearchConditions = SearchConditions + " and PRODUCTION_ORDER_ID like''" + "%" + txtPRODUCTION_ORDER_ID.Text.Trim() + "%" + "''";
- SQLgetcount = SQLgetcount + " and PRODUCTION_ORDER_ID like'" + "%" + txtPRODUCTION_ORDER_ID.Text.Trim() + "%" + "'";
- }
- if (!string.IsNullOrEmpty(txtBeginTime.Text) && !string.IsNullOrEmpty(txtEndTime.Text))
- {
- SearchConditions = SearchConditions + " and CHECK_TIME>=''" + txtBeginTime.Text + "''" + " and CHECK_TIME<=''" + txtEndTime.Text + "''";
- SQLgetcount = SQLgetcount + " and CHECK_TIME>='" + txtBeginTime.Text + "'" + " and CHECK_TIME<='" + txtEndTime.Text + "'";
- }
- if (ddlCon.Text != "9")
- {
- SearchConditions = SearchConditions + " and CONCLUSION =''" + ddlCon.Text + "''";
- SQLgetcount = SQLgetcount + " and CONCLUSION ='" + ddlCon.Text + "'";
- }
- int StartRow = (AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize) - AspNetPager1.PageSize;//計算起始行
- 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'";
- //計算總行數
- DataSet dss = DBHelper.GetDataSetMis(SQLgetcount);
- DataTable fileNameDt = dss.Tables[0];
- this.AspNetPager1.RecordCount = Convert.ToInt32(fileNameDt.Rows[0][0].ToString());//總行數
- //分頁後資料填充
- DataSet ds = DBHelper.GetDataSetMis(SQLGetPage);
- PagedDataSource pds = new PagedDataSource();
- pds.AllowPaging = true;//是否開啟分頁
- pds.AllowServerPaging = true;//是否開啟伺服器端分頁
- pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex;//當前頁的頁碼
- pds.PageSize = AspNetPager1.PageSize;//每頁顯示的行數
- int aaa = ds.DefaultViewManager.DataSet.Tables.Count;
- if (ds.DefaultViewManager.DataSet.Tables.Count == 0)//修復當查詢結果為空時“無法找到表 0。”的錯誤
- {
- pds.DataSource = null;
- }
- else
- {
- pds.DataSource = ds.Tables[0].DefaultView;
- if (ds.Tables[0].Rows.Count != AspNetPager1.PageSize)//修復最後一頁因為剩餘尾數和頁數不對應出現索引錯誤的問題
- {
- pds.PageSize = ds.Tables[0].Rows.Count;
- }
- }
- this.GridView1.DataSource = pds;
- this.GridView1.DataBind();
- }
- /// <summary>
- /// 繫結資料到分頁控制元件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void AspNetPager1_PageChanged(object sender, EventArgs e)
- {
- RowDatasBind();
- }
- /// <summary>
- /// 條件查詢
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnQuery_Click(object sender, EventArgs e)
- {
- RowDatasBind();
- }
五、總結
到這裡真假分頁以及分頁儲存過程都已經完成了,頁面載入時再也不用漫長的等待。如果你有更好方法建議歡迎吐槽、留言~