分页是Web中经常遇到的功能,分页主要有真分页和假分页。
所谓真分页是指:每一页显示多少数据,就从数据库读多少数据;
假分页是指:一次性从数据库读取所有数据,然后再进行分页。
这两种分页方式区别在于从数据库读取信息的方式,真分页的效率高。假分页在首次页面加载的时候会比较慢(如果数据量较多)。
下面学习下使用AspNetPager进行真分页
1.前台编写Repeater所呈现的数据:
class="code_img_closed" src="/Upload/Images/2014081114/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('b864593f-618d-4482-bbe3-258b566ce3f5',event)" src="/Upload/Images/2014081114/2B1B950FA3DF188F.gif" alt="" /><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; }