前言
环境:VS2019 MySQL
表格中的数据到达一定数量后,分页查看对用户的体验感有很大提升,往下看,带你实现分页功能!
步骤:
- 下载AspNetPager分页控件,地址:https://pan.baidu.com/s/1dV7dJ99Vud8JKI4Ofaqq5g
- 在VS中的工具箱和引用中分别添加该DLL文件
- 前台代码:
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%"
NumericButtonCount="8" UrlPaging="true"
CustomInfoHTML="总计%RecordCount%条记录,共%PageCount%页"
ShowCustomInfoSection="left"
FirstPageText="首页" LastPageText="末页" NextPageText="下页" PrevPageText="上页"
Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true" SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged">
</webdiyer:AspNetPager>
解释:
NumericButtonCount:一页显示的数据的数量 UrlPaging:是否通过url地址传递分页信息 ShowCustomInfoSection:设置用户自定义信息区的方式 AlwaysShow:是否总显示此分页控件,即使数据只有一页 SubmitButtonStyle:设置提交按钮的CSS样式
- UI代码:
private void BindData()
{
//这段的作用:页面加载完时没有数据显示,加上这段可以显示数据
int startRecord = 0;
int endRecord = 0;
int i = AspNetPager1.CurrentPageIndex - 1;
startRecord = 8 * i + 1; //(8为一页显示数据的数量,这的值要与前端NumericButtonCount属性的值一致。)
endRecord = startRecord + 7;
//绑定数据库数据
repUser.DataSource = new UserBLL().selectAllBLL(startRecord, endRecord);
repUser.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
//绑定数据库数据
BindData();
//查询数据的总数量
AspNetPager1.RecordCount = new UserBLL().selectCountDAO().Count;
}
- D代码:
public List<User> selectAllDAO(int startIndex,int endIndex)
{
DataTable dt = new DataTable();
MySqlParameter[] paras = new MySqlParameter[]
{
new MySqlParameter("@startIndex",startIndex),
new MySqlParameter("@endIndex",endIndex)
};
string cmdText = @"SET @row_number=0;SELECT * from (SELECT (@row_number:=@row_number+1)as 'rn',userId,userName,uLevel FROM t_user where uLevel='操作员') as temp where temp.rn BETWEEN @startIndex and @endIndex";
dt = sqlhelper.ExecuteQuery(cmdText,paras, CommandType.Text);
ConvertList convert = new ConvertList();
List<User> list = convert.DataTableToList<User>(dt);
return list;
}
tips:SQL语句解释
• 先将ROW_NUMBER初值设为0 • 查询t_user表的数据作为一个临时表temp • @row_number:=@row_number+1:行号递增,步长为1 •再查询临时表temp中的一段数据
本文介绍了如何在ASP.NET环境中实现表格数据的分页功能,提高用户体验。通过使用AspNetPager分页控件,结合VS2019和MySQL数据库,详细讲解了下载控件、添加引用到工具箱以及前后台代码的实现过程。
1270





