Repeater分页,需要依靠PagedDataSource。这个类存在于System.Web.UI.WebControls命名空间。它的作用是作为数据源与数据显示控件的中间介质。
前台代码:
<body> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <p style="background-color:#988c6e;width:400px;padding-top:5px;padding-bottom:5px;margin-left:30px;margin-top:30px;border-radius:5px;color:#fff;font-weight:bold;"><span style="padding-left:30px;">用户名</span><span style="padding-left:100px;">注册时间</span><span style="padding-left:90px;">访问量</span></p> <table style="margin-left:30px;margin-top:30px;"> </HeaderTemplate> <ItemTemplate> <tr> <td style="width:120px;text-align:left; padding-left:20px;"><%#Eval("Username") %></td> <td style="width:170px;text-align:left; "><%#Eval("RegistrationTime") %></td> <td style="width:50px;text-align:left; "><%#Eval("AccessAmount") %></td> </tr> <tr> <td colspan="3" style="border-bottom:1px inset #C0D9D9;padding-top:7px;"></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <div style="margin-left:50px;"> <div style="margin:0 auto; margin-top:50px;border:1px solid #fff;font-size:16px;font-family:"microsoft yahei","宋体";"> <a><div style="border:1px solid #000; width:60px; float:left; margin:5px;text-align:center;"><a style="color:#000">共<asp:Label runat ="server" ID="zong"> </asp:Label>页</a></div></a> <a><div style="border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;"><a style="color:#000">第<asp:Label runat ="server" ID="dangqian"> </asp:Label>页</a></div></a> <a><div style="border:1px solid #000; width:40px; float:left;margin:5px;text-align:center;"> <a style="color:#000"><asp:hyperlink id="first" runat="server" style="color:#000">首页</asp:hyperlink></a></div></a> <a><div style="border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;"><a style="color:#000"><asp:hyperlink id="lnkPrev" runat="server" style="color:#000">上一页</asp:hyperlink></a></div></a> <a><div style="border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;"><a style="color:#000"><asp:hyperlink id="lnkNext" runat="server" style="color:#000">下一页</asp:hyperlink></a></div></a> <a><div style="border:1px solid #000; width:40px; float:left;margin:5px;text-align:center;"> <a style="color:#000"><asp:hyperlink id="end" runat="server" style="color:#000">尾页</asp:hyperlink></a></div></a> </div> </div> </body>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { getUsers(); } } private void getUsers() { List<Users1> list = new AdminManager().QueryUsers(); PagedDataSource pag = new PagedDataSource(); pag.AllowPaging = true;// 设置允许分页 pag.PageSize = 10; // 每页显示为3行 pag.DataSource = list; // 模板绑定数据源 zong.Text = pag.PageCount.ToString(); // 显示总共页数 int CurrentPage; // 请求页码为不为null设置当前页,否则为第一页 if (Request.QueryString["Page"] != null) { CurrentPage = Convert.ToInt32(Request.QueryString["Page"]); } else { CurrentPage = 1; } if (Request.QueryString["PageSize"] != null) { pag.PageSize = Convert.ToInt32(Request.QueryString["PageSize"]); } else { pag.PageSize = 10; } pag.CurrentPageIndex = CurrentPage - 1; // 当前页所引为页码-1 dangqian.Text = CurrentPage.ToString(); // 当前页 if (!pag.IsFirstPage) { // Request.CurrentExecutionFilePath为当前请求虚拟路径 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1); } // 如果不是最后一页,通过参数Page设置下一页为当前页+1,否则不显示连接 if (!pag.IsLastPage) { // Request.CurrentExecutionFilePath为当前请求虚拟路径 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1); } //首页 first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1); //尾页 end.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + pag.PageCount.ToString(); if (Convert.ToInt32(HttpContext.Current.Request["page"]) > pag.PageCount) { first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1); } this.Repeater1.DataSource = pag; this.Repeater1.DataBind(); }
如果不需要进行分页,可执行以下代码:
protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { getUsers(); } } private void getUsers() { List<Users1> list = new AdminManager().QueryUsers(); this.Repeater1.DataSource = list ; this.Repeater1.DataBind(); }
时间: 2024-10-16 22:28:04