分頁是Web中經常遇到的功能,分頁主要有真分頁和假分頁。
所謂真分頁是指:每一頁顯示多少數據,就從數據庫讀多少數據;
假分頁是指:一次性從數據庫讀取所有數據,然后再進行分頁。
這兩種分頁方式區別在于從數據庫讀取信息的方式,真分頁的效率高。假分頁在首次頁面加載的時候會比較慢(如果數據量較多)。
下面學習下使用AspNetPager進行真分頁
1.前臺編寫Repeater所呈現的數據:
<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>aspx
2.加入AspNetPager控件
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true" //始終顯示分頁控件,即使至分成一頁 UrlPaging="true" //通過URL傳遞分頁信息 NumericButtonTextFormatString="[{0}]" //索引格式 ShowCustomInfoSection="Left" //顯示當前頁和總頁數信息,默認值不顯示,為left則將顯示在頁索引前,為right則為頁索引后 ShowInputBox="Always" //輸入框 TextAfterInputBox="頁" //輸入框之后 TextBeforeInputBox="跳轉到第" > //輸入框之前 </webdiyer:AspNetPager>
3.后臺分頁及綁定數據
PRotected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindRepeater(); } }
a.BindRepeater()函數,綁定數據庫等操作
public void BindRepeater() { this.AspNetPager1.PageSize = 5;//分頁大小 int count = 1;//當前頁默認為1 if (Request.QueryString["page"] != null)//如果當前頁非空,則將URL中的page賦值為當前頁的值 { count = Convert.ToInt32(Request.QueryString["page"].ToString());//使用URL傳遞分頁信息,(如果使用AspNetPager的PageChanged函數,會在函數中調用兩次PageChanged函數,影響運行效率) } int num = (count - 1) * this.AspNetPager1.PageSize; //當前頁減1,乘以每頁顯示數目,得到前幾頁的數據數量 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語句,查找當前頁的數據 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 = "記錄總數:<b>" + AspNetPager1.RecordCount.ToString() + "</b>"; AspNetPager1.CustomInfoHTML += " 總頁數:<b>" + AspNetPager1.PageCount.ToString() + "</b>"; AspNetPager1.CustomInfoHTML += " 當前頁:<font color=/"red/"><b>" + count + "</b></font>"; }
b.GetPage函數,返回數據集
/// <summary> /// 獲得數據源 /// </summary> /// <param name="sql">sql語句</param> /// <param name="currentPage">當前頁</param> /// <param name="pagesize">分頁大小</param> /// <param name="recordcount">總頁數</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");//對讀取到的數據進行分頁,假分頁時可以這樣操作 ada.Fill(ds, "table"); //填充 recordcount = GetPageRecord();//得到總頁數 return ds; }
c.GetPagRecord函數,獲得總記錄數
/// <summary> /// 獲得總記錄數 /// </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; }
新聞熱點
疑難解答