欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET中的repeater简介及分页效果

程序员文章站 2022-06-20 13:35:47
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> 
 <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();
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!