Brief introduction of repeater in. NET and paging effect

  • 2021-09-11 20:01:05
  • OfStack

The Repeater control is a data-bound container control that generates a list of items and uses templates to define the layout of items on a Web page. When the page runs, the control repeats this layout for each item in the data source.

Using repeater controls with templates

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

ItemTemplate: Contains HTML elements and controls to render once for each data item in the data source.

AlternatingItemTemplate: Formatting alternating data items (contains HTML elements and controls that are rendered once for each data item in the data source. Typically, you can use this template to create a different look for alternating items, such as specifying a background color different from that specified in ItemTemplate).

SeparatorTemplate: Formatting the separator (containing the elements rendered between each item.) .

HeaderTemplate: Formatting the header (containing text and controls rendered separately at the beginning of the list.) .

FooterTemplate: Formatting the footer (containing text and controls rendered at the end of the list, respectively.) .      

Repeater paging results as follows:

Foreground 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;"> User name </span><span style="padding-left:100px;"> Registration time </span><span style="padding-left:90px;"> Visit volume </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"," Song Style ";">
  <a><div style="border:1px solid #000; width:60px; float:left; margin:5px;text-align:center;"><a style="color:#000"> Altogether <asp:Label runat ="server" ID="zong"> </asp:Label> Page </a></div></a>
  <a><div style="border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;"><a style="color:#000"> No. 1 <asp:Label runat ="server" ID="dangqian"> </asp:Label> Page </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"> Home page </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"> Upper 1 Page </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"> Under 1 Page </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"> End page </asp:hyperlink></a></div></a>
  </div> 
 </div> 
 </body>

Background 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;//  Set allowed paging 
  pag.PageSize = 10; //  Each page is displayed as 3 Row 
  pag.DataSource = list; //  Template bound data source  
  zong.Text = pag.PageCount.ToString(); //  Show total pages 
  int CurrentPage;
  //  Request page number is not null Sets the current page, otherwise, the 1 Page 
  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; //  The current page is quoted as a page number -1
  dangqian.Text = CurrentPage.ToString(); //  Current page 
  if (!pag.IsFirstPage)
  {
  //  Request.CurrentExecutionFilePath Virtual path for the current request 
  lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
  } 
  //  If it's not the last 1 Page, using the parameter Page Set under 1 Page is the current page +1 Otherwise, the connection is not displayed 
  if (!pag.IsLastPage)
  {
  // Request.CurrentExecutionFilePath Virtual path for the current request 
  lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
  }
  // Home page 
  first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
  // End page 
  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 you don't need paging, 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 articles: