1. 首先应该添加引用到bin文件夹下:
2. 在页面中添加:
<%@ Register Assembly="AspNetPager" Namespace="" TagPrefix="webdiyer" %>。
3. 将bin文件夹下的添加到工具栏,拖拽至页面中:<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 = ())
{
();
= "select * from articleinfo";
totalpagesize = Convert.ToInt32(());//获取总的数据行数
this. = totalpagesize;
= "ProcarticleInfo";
//表明调用的是存储过程
= ;
("@PAGESIZE", pagesize);
("@PAGEINDEX", pageindex);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
(dt);
this. = dt;
this.();
}
}
8.调用page_changed的方法使更换页码
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
this. = pagesize;
pageindex= this.;
DataBindArticle();
}