Repeater控件是一个数据绑定容器控件,它能够生成各个项的列表,并能够运用模板定义网页上各个项的规划。当该页运行时,该控件为数据源中的每一个项反复此规划。
合营模板运用repeater控件
若要运用repeater控件,需建立定义控件内容规划的模板。模板能够包括标记和控件的恣意组合。假如未定义模板,或许模板都不包括元素,则当应用程序运行时,该控件不显现在页面上。
ItemTemplate : 含要为数据源中每一个数据项都要显现一次的 HTML 元素和控件。
AlternatingItemTemplate : 对交替数据项举行花样设置(包括要为数据源中每一个数据项都要显现一次的 HTML 元素和控件。一般,能够运用此模板为交替项建立差别的表面,比方指定一种与在ItemTemplate中指定的色彩差别的背景色)。
SeparatorTemplate : 对分隔符举行花样设置(包括在每项之间显现的元素。)。
HeaderTemplate : 对页眉举行花样设置(包括在列表的最先处离别显现的文本和控件。)。
FooterTemplate : 对页脚举行花样设置(包括在列表的完毕处离别显现的文本和控件。)。
Repeater分页结果以下:
前台代码:
<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> <p style="margin-left:50px;"> <p style="margin:0 auto; margin-top:50px;border:1px solid #fff;font-size:16px;font-family:"microsoft yahei","宋体";"> <a><p 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></p></a> <a><p 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></p></a> <a><p 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></p></a> <a><p 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></p></a> <a><p 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></p></a> <a><p 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></p></a> </p> </p> </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(); }
【相干引荐】
1. ASP免费视频教程
2. ASP教程
3. 李炎恢ASP基本视频教程
以上就是关于repeater和分页结果的代码详解(ASP)的细致内容,更多请关注ki4网别的相干文章!