ASP.NET使用Reperater控件实现数据库动态加载,使用分页展示数据(源码)
先展示一下效果
1、首先编写页面代码,画出表格
我在画表格的时候用了pure框架,大家看个人喜好
<table class=\"pure-table pure-table-striped pure-table-bordered\"><thead><tr><th>编号</th><th>音乐名称</th><th>类型名称</th><th>歌手名称</th><th>音乐价格</th><th>操作</th></tr></thead><tbody><asp:Repeater ID=\"rpAlbums\" runat=\"server\"><ItemTemplate><tr><td><%# Eval(\"AlbumId\") %></td><td><%# Eval(\"Title\") %></td><td><%# Eval(\"Genres.Name\")%></td><td><%# Eval(\"Artists.Name\")%></td><td><%# Eval(\"Price\",\"{0:C}\")%></td><td><asp:LinkButton ID=\"btnUpdate\" CommandArgument=\'<%# Eval(\"AlbumId\") %>\' CssClass=\"pure-button pure-button-primary\" runat=\"server\" Text=\"编辑\" OnClick=\"btnUpdate_Click\" /><asp:LinkButton ID=\"btnDelete\" CommandArgument=\'<%# Eval(\"AlbumId\") %>\' CssClass=\"button-default pure-button\" runat=\"server\" Text=\"删除\" OnClientClick=\"return confirm(\'您确定要删除嘛?\')\" OnClick=\"btnDelete_Click\"/></td></tr></ItemTemplate></asp:Repeater><tr><td colspan=\"6\"><asp:Label ID=\"lblpage\" runat=\"server\" Text=\"Label\"></asp:Label><asp:LinkButton ID=\"btnFirst\" runat=\"server\" CssClass=\"pure-button\" OnClick=\"btnFirst_Click\">首页</asp:LinkButton><asp:LinkButton ID=\"btnPrev\" runat=\"server\" CssClass=\"pure-button\" OnClick=\"btnPrev_Click\">上一页</asp:LinkButton><asp:LinkButton ID=\"btnNext\" runat=\"server\" CssClass=\"pure-button\" OnClick=\"btnNext_Click\">下一页</asp:LinkButton><asp:LinkButton ID=\"btnLast\" runat=\"server\" CssClass=\"pure-button\" OnClick=\"btnLast_Click\">尾页</asp:LinkButton></td></tr></tbody></table>
2、页面画好之后,我们编写后台代码
编写load事件,给repeater控件绑定数据
protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){loaddata();}}private void loaddata(){using (OLMSDBEntities db = new OLMSDBEntities()){var data = db.Albums.Include(\"Artists\").Include(\"Genres\").ToList();//分页功能rpAlbums.DataSource = data.Skip((PageNmuber - 1) * PageSize).Take(PageSize);rpAlbums.DataBind();//总页数PageCount = Convert.ToInt32(Math.Ceiling(data.Count / (PageSize * 1.0)));lblpage.Text = $\"每页{PageSize}条/共{data.Count}条,当前{PageNmuber}页/共{PageCount}页\";//按钮启用条件btnFirst.Enabled = !(PageNmuber <= 1);btnPrev.Enabled = !(PageNmuber <= 1);btnNext.Enabled = !(PageNmuber >= PageCount);btnLast.Enabled = !(PageNmuber >= PageCount);}}
分页的具体代码:
//页码public int PageNmuber {get{if (ViewState[\"page\"]==null){return 1;}else{return Convert.ToInt32(ViewState[\"page\"]);}}set{ViewState[\"page\"] = value;}}//每页记录数public int PageSize { get { return 5; } }//总页数public int PageCount {get{if (ViewState[\"count\"] == null){return 1;}else{return Convert.ToInt32(ViewState[\"count\"]);}}set{ViewState[\"count\"] = value;}}
首页,上一页,下一页,尾页的点击事件
//首页protected void btnFirst_Click(object sender, EventArgs e){PageNmuber = 1;loaddata();}//上一页protected void btnPrev_Click(object sender, EventArgs e){PageNmuber--;loaddata();}//下一页protected void btnNext_Click(object sender, EventArgs e){PageNmuber++;loaddata();}//尾页protected void btnLast_Click(object sender, EventArgs e){PageNmuber = PageCount;loaddata();}
到此就结束啦,快去练习一下吧!欢迎大佬和小Monkey沟通。
感谢大佬指正 小Monkey
如果你觉得有用的话,就留个赞吧!蟹蟹