分页是Web中经常遇到的功能,分页主要有真分页和假分页。
所谓真分页是指:每一页显示多少数据,就从数据库读多少数据;
假分页是指:一次性从数据库读取所有数据,然后再进行分页。
这两种分页方式区别在于从数据库读取信息的方式,真分页的效率高。假分页在首次页面加载的时候会比较慢(如果数据量较多)。
下面学习下使用AspNetPager进行真分页
1.前台编写Repeater所呈现的数据:
<table width="" border="">
<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="" width="">
<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
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzIAAAC9CAIAAADnWd/qAAARxUlEQVR4nO3dS7IqxxUFUCI0MbU9E8KTuONyRzTVVMMDsMagARw3sOuW8kdCFpCXXCsqXkCRFOjp5GHXB94pAIBpfH19vfst8Dand78BAOCbWLay/8WyL3jI5XJRQmAiMG5fRa9PA0ziO5a9933wQ+kjECYCR1BFhFjGIH0EwkTgCKqIEMsYpI9AmAgcQRURxVh2Pp+TQfma/kf5bI0+khTG+XyulUpPCW1j7q039ckL+EBlnCoixmOZz7zF5X3k/H/Fu0V5gGuMqT3adud/FtynOBH2Ax4oQnW7GrGMuBnLrrfbsczn38oe+zS6maLaG7mrzNQkL1D8QO2s26TsddRliWVEEsvydvC8D0s+Q+No2c1Mv42/+So3C6+R7ZQlLzDygXr++5Hg5G78/ZCz0PbBxDIiP1qWNIJk5V6tg7CUxtGye5N9rYQa5ddDZfICyURICru2z7AN2N8oNtVap1Xen0QsI9qxbBuUz/y86TzxPTKxnliWrN/0fMA06jC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DT5jZP9GetSu/WqMiSxdNU50dka017jrnOxsxDIAgCmc/vXPsFgsFovFYrG8fRHLLBaLxWKxWKZYTn/++R+LxWKxWCwWy9uX09vfgcVisVgsFovlzz//818MXTDNlUbl+wAAAABJRU5ErkJggg==" alt="" />
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 = ;//分页大小
int count = ;//当前页默认为1 if (Request.QueryString["page"] != null)//如果当前页非空,则将URL中的page赋值为当前页的值
{
count = Convert.ToInt32(Request.QueryString["page"].ToString());//使用URL传递分页信息,(如果使用AspNetPager的PageChanged函数,会在函数中调用两次PageChanged函数,影响运行效率)
}
int num = (count - ) * 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;
}