Code explanation for repeater and paging effects (ASP)

Source: Internet
Author: User
The Repeater control is a data-bound container control that generates a list of items and can use templates to define the layout of individual items on a Web page. This article is described in detail, following the small series together to see it

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

Working with templates using repeater controls

To use the Repeater control, you create a template that defines the layout of the control's content. A template can contain any combination of tags and controls. If no template is defined, or the template does not contain elements, the control does not appear on the page when the application is run.

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

AlternatingItemTemplate: Formats alternating data items that contain 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 skins for alternating items, such as specifying a background color that differs from the color specified in ItemTemplate.

SeparatorTemplate: The delimiter is formatted (contains elements that are rendered between each item). )。

HeaderTemplate: Formats the header containing the text and controls that are rendered separately at the beginning of the list. )。

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

The repeater pagination effect is as follows:

Front 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;" > Visits </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 I Nset #C0D9D9;p adding-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", "Song Body"; " > <a><p style= "border:1px solid #000; width:60px; Float:left; Margin:5px;text-align:center; " ><a style= "color: #000" > Total <asp:label runat = "server" id= "zong" > </asp:Label> page </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> page </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" > Home </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" > Previous </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" > Next </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" > Last </asp: hyperlink></a></p></a> </p> </p> </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;//setting allows paging pag. PageSize = 10; Each page is displayed as a 3-line pag. DataSource = list; The template binds the data source Zong. Text = pag. Pagecount.tostring ();  Displays the total number of pages int currentpage; The request page number is not set to NULL for the current page, otherwise the first 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 cited as page number-1 Dangqian. Text = Currentpage.tostring (); The current page if (!pag. Isfirstpage) {//Request.currentexecutionfilepath for the current request virtual path Lnkprev.navigateurl = Request.currentexecutionfilepath + " ?  Page= "+ convert.tostring (CurrentPage-1);}//If it is not the last page, set the next page by the parameter page to the current page +1, otherwise the connection if (!PAG) is not displayed. Islastpage) {//Request.currentexecutionfilepath for the current request virtual path Lnknext.navigateurl = Request.currentexecutionfilepath + "?  Page= "+ convert.tostring (currentpage + 1); }//Home first. NAVIGATEURL = Request.currentexecutionfilepath + "?  Page= "+ convert.tostring (1); End 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, the following code can be executed:

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

"Recommended"

1. asp free Video Tutorial

2. asp Tutorials

3. Eon the ASP Basic video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.