標籤: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