AspNetPager真假分頁對比執行個體

來源:互聯網
上載者:User

標籤:asp.net   網頁設計   介面   分頁   

    從開始學習BS已經有一段時間了。對於BS的設計,都是進行的網頁設計,其中包括從資料庫中取出來的資料,顯示在頁面上。以前在CS中,都是使用GridView等表格控制項進行顯示,由於資料小,而且右側又有滾輪。所以從來都沒有考慮過給資料進行分頁。所以這是一大失誤。現在反應過來也不算晚。接下來,讓我們接觸一下分頁。

 

不進行分頁的弊端:

對於大資料的查詢操作。

   1、資料量太大,表單載入時間太慢

   2、顯示的資料量太大,使用者看起來不舒服。

 

分頁的兩種方式:

 

假分頁:

在執行查詢的時候,就把所有的資料都查詢出來,之後再跳轉頁面的時候就不需要進行查詢了。

 

真分頁:

在執行查詢操作的時候,只查詢所顯示當前頁面的內容進行顯示。之後的跳轉頁面也需要查詢資料庫取對應資料。

 

在資料量大的情況下對比:

 

   在小資料的情況下,因為現在的電腦處理速度都夠快,採用真分頁和假分頁使用者體驗度沒有差別。

   關於如何添加AspNetPager控制項,借閱以下文章:

    http://blog.csdn.net/u010066934/article/details/38374111

 

真假分頁執行個體結果圖:

 

   使用內建的GridView表格來顯示資料。利用AspNetPager控制項  進行分頁。

   如何使用AspNetPager請翻閱---。我就不贅述了。

 

html主介面:

<table class="auto-style1">                <tr>                    <td>                        <webdiyer:AspNetPager ID="anpTop" runat="server" PageSize="5" OnPageChanged="anp_PageChanged">                        </webdiyer:AspNetPager>                    </td>                </tr>                <tr>                    <td class="auto-style2">                        <asp:GridView ID="GridView1" runat="server">                        </asp:GridView>                    </td>                </tr>                <tr>                    <td>                        <webdiyer:AspNetPager ID="anpBottom" runat="server" CloneFrom="anpTop" CssClass="anp" OnPageChanged="anp_PageChanged" PageSize="5">                        </webdiyer:AspNetPager>                    </td>                </tr>            </table>


先設定一個分頁控制項在首部,之後再複製上面的控制項放到底部,使其達到同步的效果。

 

為了對比一下真假分頁,首先看假分頁的實現:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;namespace news{    public partial class JiaFenYe : System.Web.UI.Page    {        DataView dv = new DataView(DB.GetSource());        protected void Page_Load(object sender, EventArgs e)        {            //anpTop.RecordCount = DB.count();            //把資料的記錄總數 儲存到分頁控制項中            //GridView1.DataSource = DB.GetSource(1, 5);   //預設選擇5條記錄            //GridView1.DataBind();            anpTop.RecordCount = DB.count();            //把資料的記錄總數 儲存到分頁控制項中            dv.RowFilter = "id<=5";             //預設第一頁顯示5條記錄            GridView1.DataSource = dv;            GridView1.DataBind();           //綁定視圖        }        protected void anp_PageChanged(object sender, EventArgs e)        {            //Response.Write("開始頁碼:" + anpTop.StartRecordIndex + "<br> 結束頁碼:" + anpTop.EndRecordIndex);            int startIndex = anpTop.StartRecordIndex;       //其實記錄號            int endIndex = anpTop.EndRecordIndex;           //終止記錄號            //DataView dv = new DataView(DB.GetSource());            dv.RowFilter = "id >=" + startIndex + " and id<= " + endIndex;      //查詢指定區間內的記錄            GridView1.DataSource = dv;            GridView1.DataBind();           //綁定視圖        }    }}


 

在來對比一下,真分頁的實現:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace news{    public partial class WebForm1 : System.Web.UI.Page    {        protected void anp_PageChanged(object sender, EventArgs e)        {            //Response.Write("開始頁碼:" + anpTop.StartRecordIndex + "<br> 結束頁碼:" + anpTop.EndRecordIndex);            int startIndex = anpTop.StartRecordIndex;       //資料的起始記錄數            int endIndex = anpTop.EndRecordIndex;           //資料的截止記錄數            GridView1.DataSource = DB.GetSource(startIndex,endIndex);   //顯示當前區間內的資料            GridView1.DataBind();        }        protected void Page_Load(object sender, EventArgs e)        {            anpTop.RecordCount = DB.count();            //把資料的記錄總數 儲存到分頁控制項中            GridView1.DataSource = DB.GetSource(1,5);   //預設選擇5條記錄            GridView1.DataBind();        }    }}

 

   通過對比以上實驗結果,因為測試的資料太少  效果不明顯。不過通過代碼實現過程,可以看出,在大資料的前提下,假分頁載入是一個很大的問題。當然 兩種分頁方式 各有特色,可以根據實際需求來選擇。

    如果有需要看源碼的,請自行下載:

    http://download.csdn.net/detail/zc474235918/7734743


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.