分頁是Web中經(jīng)常遇到的功能,分頁主要有真分頁和假分頁。
所謂真分頁是指:每一頁顯示多少數(shù)據(jù),就從數(shù)據(jù)庫讀多少數(shù)據(jù);
假分頁是指:一次性從數(shù)據(jù)庫讀取所有數(shù)據(jù),然后再進(jìn)行分頁。
這兩種分頁方式區(qū)別在于從數(shù)據(jù)庫讀取信息的方式,真分頁的效率高。假分頁在首次頁面加載的時候會比較慢(如果數(shù)據(jù)量較多)。
下面學(xué)習(xí)下使用AspNetPager進(jìn)行真分頁
1.前臺編寫Repeater所呈現(xiàn)的數(shù)據(jù):

<table width="650" border="1"> <tr> <td class="tr1"> <asp:Label Text="姓名" runat="server"></asp:Label> </td> <td class="tr2"> <asp:Label Text="所在公司" runat="server"></asp:Label> </td> <td class="tr3"> <asp:Label Text="注冊ID" runat="server"></asp:Label> </td> </tr> </table> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <table border="1" width="650"> <tr> <td class="tr1"> <%#Eval("E_Id")%> </td> <td class="tr2"> <%#Eval("C_Id") %> </td> <td class="tr3"> <%#Eval("User_Id") %> </td> </tr> </table> </ItemTemplate> </asp:Repeater> aspx2.加入AspNetPager控件
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true" //始終顯示分頁控件,即使至分成一頁 UrlPaging="true" //通過URL傳遞分頁信息 NumericButtonTextFormatString="[{0}]" //索引格式 ShowCustomInfoSection="Left" //顯示當(dāng)前頁和總頁數(shù)信息,默認(rèn)值不顯示,為left則將顯示在頁索引前,為right則為頁索引后 ShowInputBox="Always" //輸入框 TextAfterInputBox="頁" //輸入框之后 TextBeforeInputBox="跳轉(zhuǎn)到第" > //輸入框之前 </webdiyer:AspNetPager> 3.后臺分頁及綁定數(shù)據(jù)
PRotected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindRepeater(); } }
a.BindRepeater()函數(shù),綁定數(shù)據(jù)庫等操作
public void BindRepeater() { this.AspNetPager1.PageSize = 5;//分頁大小 int count = 1;//當(dāng)前頁默認(rèn)為1 if (Request.QueryString["page"] != null)//如果當(dāng)前頁非空,則將URL中的page賦值為當(dāng)前頁的值 { count = Convert.ToInt32(Request.QueryString["page"].ToString());//使用URL傳遞分頁信息,(如果使用AspNetPager的PageChanged函數(shù),會在函數(shù)中調(diào)用兩次PageChanged函數(shù),影響運(yùn)行效率) } int num = (count - 1) * this.AspNetPager1.PageSize; //當(dāng)前頁減1,乘以每頁顯示數(shù)目,得到前幾頁的數(shù)據(jù)數(shù)量 string sql = "select top " + this.AspNetPager1.PageSize + " * from Emp where E_Id not in (" + " select top " + num + " E_Id from Emp order by E_Id asc) order by E_Id asc";//自定義的SQL語句,查找當(dāng)前頁的數(shù)據(jù) int recordcount; DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount); this.AspNetPager1.RecordCount = recordcount; Repeater1.DataSource = ds; Repeater1.DataBind(); AspNetPager1.CustomInfoHTML = "記錄總數(shù):<b>" + AspNetPager1.RecordCount.ToString() + "</b>"; AspNetPager1.CustomInfoHTML += " 總頁數(shù):<b>" + AspNetPager1.PageCount.ToString() + "</b>"; AspNetPager1.CustomInfoHTML += " 當(dāng)前頁:<font color=/"red/"><b>" + count + "</b></font>"; }b.GetPage函數(shù),返回數(shù)據(jù)集
/// <summary> /// 獲得數(shù)據(jù)源 /// </summary> /// <param name="sql">sql語句</param> /// <param name="currentPage">當(dāng)前頁</param> /// <param name="pagesize">分頁大小</param> /// <param name="recordcount">總頁數(shù)</param> /// <returns>DataSet</returns> public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount) { // String strSql = "select * from Emp"; SqlDataAdapter ada = new SqlDataAdapter(sql, GetConnection()); DataSet ds = new DataSet(); //int startRow = (currentPage - 1) * pagesize; //ada.Fill(ds, startRow, pagesize, "table");//對讀取到的數(shù)據(jù)進(jìn)行分頁,假分頁時可以這樣操作 ada.Fill(ds, "table"); //填充 recordcount = GetPageRecord();//得到總頁數(shù) return ds; } c.GetPagRecord函數(shù),獲得總記錄數(shù)
/// <summary> /// 獲得總記錄數(shù) /// </summary> /// <param name="sql"></param> /// <returns></returns> public int GetPageRecord() { String sql = "select count(*) from Emp"; SqlCommand cmd = new SqlCommand(sql, GetConnection()); cmd.Connection.Open(); int recordcount = (int)cmd.ExecuteScalar(); return recordcount; }d.GetConnection,獲得連接串
public SqlConnection GetConnection() { SqlConnection conn = new SqlConnection("server=.;database=ComInfo;integrated security=true"); return conn; }新聞熱點(diǎn)
疑難解答