repeater控件是一个数据绑定容器控件,它能够生成各个项的列表,并可以使用模板定义网页上各个项的布局。本文对此进行详细介绍,下面跟着小编一起来看下吧
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. 特别推荐:“php程序员工具箱”v0.1版本下载
2. asp免费视频教程
3. 李炎恢asp基础视频教程
以上就是.net用repeater实现分页效果的代码详解的详细内容。