Controls (9) -- repeater controls implement pagination

Source: Internet
Author: User

The Repeater control has five templates, which can be selected as needed:

 

1. itemtempplate: normal item

2. alternatingitemtemplate: staggered items

3. headertemplate: Header

4. footertemplate: footer

5. separatertemplate: Separator

 

Next, let's look at an example of pagination implemented by the Repeater control:

I. design interface:

 

Ii. Front-end code:

 

<Form ID = "form1" runat = "server"> <Div align = "center" style = "height: 379px; width: 780px "> <br/> page display: <br/> current page: <asp: label id = "label1" runat = "server" text = "label"> </ASP: Label> <br/> <asp: button id = "btnbefore" runat = "server" text = "Previous Page" font-bold = "true" font-size = "medium" onclick = "btnbefore_click"/> <asp: button id = "btnnext" runat = "server" text = "next page" font-bold = "true" font-size = "medium" onclick = "btnnext_click"/> <br/> <br/> <% -- Panel container -- %> <asp: panel id = "Panel1" runat = "server" Height = "180px" width = "441px"> <% -- Repeater control -- %> <asp: repeater id = "repeater1" runat = "server"> <% -- normal -- %> <itemtemplate> <% # databinder. eval (container. dataitem, "cityid") %> <% -- BIND database Field -- %> <% # databinder. eval (container. dataitem, "cityname ") %> </itemtemplate> 

3. Create a database:

Create Database City -- create database use citycreate table City -- create a city table (cityid int primary key, proid int foreign key references province (proid), cityname varchar (50) not null) insert into city values (, 'beijing') insert into city values (, 'changchun ') insert into city values (, 'Jilin') insert into city values, 'heilongjiang ') insert into city values (5, 3, 'liaoning') insert into city values (6, 1, 'income ')

 

Iv. Background code:

1. Create a dbcon class for database connection:

/// <Summary> // database connection // </Summary> public class dbcon {public static sqlconnection createcon () {sqlconnection con = new sqlconnection ("Server = .; database = city; user id = sa; Pwd = 123456 "); Return con ;}}

 

2. Background code:

Protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {This. label1.text = "1"; // set the start page to the first page. databindtorepeater (); // bind data} // <summary> // bind /// </Summary> private void databindtorepeater () {int curpage = convert. toint32 (this. label1.text); sqlconnection con = dbcon. createcon (); sqldataadapter SDA = new sqldataadapter (); SDA. selectcommand = new sqlcommand ("select * from city", con); dataset DS = new dataset (); SDA. fill (DS, "C Ity "); system. web. UI. webcontrols. pageddatasource PS = new pageddatasource (); PS. datasource = Ds. tables ["city"]. defaultview; // sets the view of the paging data source ps. allowpaging = true; // enable pagination ps. pagesize = 2; // set the number of items displayed on a single page ps. currentpageindex = curpage; // the index of the current page is the enabled attribute of the // control button starting from scratch. This. btnbefore. enabled = true; this. btnnext. enabled = true; if (1 = curpage) {This. btnbefore. enabled = false;} If (PS. pagecount = curpage) {This. btnnext. enabled = false;} // bind the control this. repeater1.datasource = Ps; this. repeater1.databind ();} /// <summary> /// next // </Summary> /// <Param name = "sender"> </param> /// <Param name =" E "> </param> protected void btnbefore_click (Object sender, eventargs e) {This. label1.text = convert. tostring (convert. toint32 (this. label1.text)-1); this. databindtorepeater ();} /// <summary> /// previous // </Summary> /// <Param name = "sender"> </param> /// <Param name =" E "> </param> protected void btnnext_click (Object sender, eventargs e) {This. label1.text = convert. tostring (convert. toint32 (this. label1.text) + 1); this. databindtorepeater ();}

 

5. Display Results:

 

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.