1. 首先应该添加引用到bin文件夹下:AspNetPager.dll
2. 在页面中添加:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>。
3. 将bin文件夹下的AspNetPager.dll添加到工具栏,拖拽至页面中:<webdiyer:AspNetPager ID="AspNetPager1" runat="server"> </webdiyer:AspNetPager>
4. 为AspNetPager1添加属性:
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanged="AspNetPager1_PageChanged" ShowCustomInfoSection="left" ShowInputBox="Auto" AlwaysShow="True" CustomInfoHTML="<font color='#333333'>共 %RecordCount% 行/每页%PageSize%行第%CurrentPageIndex%/%PageCount%页顺序 %StartRecordIndex%-%EndRecordIndex%</font> " NumericButtonCount="10" FirstPageText="首页" LastPageText="末页" NextPageText="下页" PrevPageText="上页" CustomInfoSectionWidth="65%" CssClass="page_text" ShowBoxThreshold="2" InputBoxClass="pagetext" SubmitButtonClass="BtnStyle" SubmitButtonText="Go" PageSize="10">
</webdiyer:AspNetPager>
标红的参数是要与后台交互的数据。
5. 在后台的定义变量
private int pagesize = 10;//每页的行数
private int totalpagesize = 0;//总的页数
private int pageindex = 1;//页码
6. 使用方法读取数据填充到Gridview中。
7. using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "select * from articleinfo";
totalpagesize = Convert.ToInt32(cmd.ExecuteScalar());//获取总的数据行数
this.AspNetPager1.RecordCount = totalpagesize;
cmd.CommandText = "ProcarticleInfo";
//表明调用的是存储过程
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PAGESIZE", pagesize);
cmd.Parameters.AddWithValue("@PAGEINDEX", pageindex);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
8.调用page_changed的方法使更换页码
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
this.AspNetPager1.PageSize = pagesize;
pageindex= this.AspNetPager1.CurrentPageIndex;
DataBindArticle();
}