search
HomeBackend DevelopmentC#.Net TutorialDetailed code explanation of .NET using repeater to achieve paging effect

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. This article introduces this in detail. Let’s take a look at it with the editor.

The Repeater control is a data binding container control. It can generate a list of each item and can use templates to define the properties of each item on the web page. layout. 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.).

RepeaterPagingThe effect is as follows:

Front desk code:


<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>

Backend code:


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();
 }

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<Users1> list = new AdminManager().QueryUsers(); 
 this.Repeater1.DataSource = list ;
 this.Repeater1.DataBind();
}

【Related recommendations】

1. Special recommendation“php "Programmer Toolbox" V0.1 version download

2. ASP free video tutorial

3. Li Yanhui ASP basic video tutorial

The above is the detailed content of Detailed code explanation of .NET using repeater to achieve paging effect. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
PHP开发:如何实现表格数据排序和分页功能PHP开发:如何实现表格数据排序和分页功能Sep 20, 2023 am 11:28 AM

PHP开发:如何实现表格数据排序和分页功能在进行Web开发中,处理大量数据是一项常见的任务。对于需要展示大量数据的表格,通常需要实现数据排序和分页功能,以提供良好的用户体验和优化系统性能。本文将介绍如何使用PHP实现表格数据的排序和分页功能,并给出具体的代码示例。排序功能实现在表格中实现排序功能,可以让用户根据不同的字段进行升序或降序排序。以下是一个实现表格

如何在CakePHP中创建自定义分页?如何在CakePHP中创建自定义分页?Jun 04, 2023 am 08:32 AM

CakePHP是一个强大的PHP框架,为开发人员提供了很多有用的工具和功能。其中之一是分页,它可以帮助我们将大量数据分成几页,从而简化浏览和操作。默认情况下,CakePHP提供了一些基本的分页方法,但有时你可能需要创建一些自定义的分页方法。这篇文章将向您展示如何在CakePHP中创建自定义分页。步骤1:创建自定义分页类首先,我们需要创建一个自定义分页类。这个

分享几个.NET开源的AI和LLM相关项目框架分享几个.NET开源的AI和LLM相关项目框架May 06, 2024 pm 04:43 PM

当今人工智能(AI)技术的发展如火如荼,它们在各个领域都展现出了巨大的潜力和影响力。今天大姚给大家分享4个.NET开源的AI模型LLM相关的项目框架,希望能为大家提供一些参考。https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel是一种开源的软件开发工具包(SDK),旨在将大型语言模型(LLM)如OpenAI、Azure

如何使用 JavaScript 实现表格分页功能?如何使用 JavaScript 实现表格分页功能?Oct 20, 2023 pm 06:19 PM

如何使用JavaScript实现表格分页功能?随着互联网的发展,越来越多的网站都会使用表格来展示数据。在一些数据量较大的情况下,需要将数据进行分页展示,以提升用户体验。本文将介绍如何使用JavaScript实现表格分页功能,并提供具体的代码示例。一、HTML结构首先,我们需要准备一个HTML结构来承载表格和分页按钮。我们可以使用&lt;tab

C#的就业前景如何C#的就业前景如何Oct 19, 2023 am 11:02 AM

无论您是初学者还是有经验的专业人士,掌握C#将为您的职业发展铺平道路。

使用JavaScript实现表格数据的分页显示使用JavaScript实现表格数据的分页显示Jun 16, 2023 am 10:00 AM

随着数据的不断增长,表格显示变得更加困难。大多数情况下,表格中的数据量过大,导致表格在加载时变得缓慢,而且用户需要不断地浏览页面才能找到自己想要的数据。本文将介绍如何使用JavaScript实现表格数据的分页显示,让用户更容易找到自己想要的数据。一、动态创建表格为了使分页功能更加可控,需要动态创建表格。在HTML页面中,添加一个类似于下面的table元素。

Vue技术开发中如何实现分页功能Vue技术开发中如何实现分页功能Oct 09, 2023 am 09:06 AM

Vue是一种流行的JavaScript框架,用于构建用户界面。在Vue技术开发中,实现分页功能是常见的需求。本文将介绍如何使用Vue来实现分页功能,并提供具体代码示例。在开始之前,我们需要提前准备一些基本知识。首先,我们需要了解Vue的基本概念和语法。其次,我们需要知道如何使用Vue组件来构建我们的应用程序。开始之前,我们需要在Vue项目中安装一个分页插件,

VUE3开发入门教程:使用组件实现分页VUE3开发入门教程:使用组件实现分页Jun 16, 2023 am 08:48 AM

VUE3开发入门教程:使用组件实现分页分页是一个常见的需求,因为在实际开发中,我们往往需要将大量的数据分成若干页以展示给用户。在VUE3开发中,可以通过使用组件实现分页功能,本文将介绍如何使用组件实现简单的分页功能。1.创建组件首先,我们需要创建一个分页组件,使用“vuecreate”命令创建VUE项目,并在src/components目录下创建Pagin

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor