Introduction to repeater and paging effect in .NET

高洛峰
Release: 2017-02-08 09:08:49
Original
1424 people have browsed it

The Repeater control is a data-binding container control that can generate a list of each item and can use templates to define the layout of each item on the web page. When the page runs, the control repeats this layout for each item in the data source.

Use the repeater control with a template

To use the repeater control, you need to create a template that defines the content layout of the control. Templates can contain any combination of markup and controls. If the template is not defined, or if the template contains no elements, the control does not appear on the page when the application runs.

ItemTemplate: Contains HTML elements and controls that are rendered once for each data item in the data source.

AlternatingItemTemplate : Format alternating data items (contains HTML elements and controls to be rendered once for each data item in the data source. Typically, you can use this template to create different appearances for alternating items, For example, specify a background color that is different from the color specified in the ItemTemplate).

SeparatorTemplate : Format the separator (the element contained between each item that is rendered.).

HeaderTemplate : Format the header (contains text and controls that are rendered separately at the beginning of the list.).

FooterTemplate : Format the footer (contains text and controls that are rendered separately at the end of the list.).

Repeater paging effect is as follows:

Foreground code:


 
  
  

用户名注册时间访问量

<%#Eval("Username") %> <%#Eval("RegistrationTime") %> <%#Eval("AccessAmount") %>
Copy after login

Backend code:

protected void Page_Load(object sender, EventArgs e)
 {
  if(!Page.IsPostBack)
  {
  getUsers();
  }
 }
 private void getUsers()
 {
  List 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();
 }
Copy after login

If paging is not required, you can execute the following code:

protected void Page_Load(object sender, EventArgs e)
{
  if(!Page.IsPostBack)
 {
   getUsers();
 }
}
private void getUsers()
{
  List list = new AdminManager().QueryUsers(); 
 this.Repeater1.DataSource = list ;
 this.Repeater1.DataBind();
}
Copy after login

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website!

For more articles related to the introduction of repeater and paging effect in .NET, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!