控件(九)——Repeater控件实现分页

Repeater控件有五个模版,我们可以根据需要选择使用,分别是:

 

1、ItemTempplate:正常项

2、AlternatingItemTemplate:交错项

3、HeaderTemplate:页眉

4、FooterTemplate:页脚

5、SeparaterTemplate:分隔符

 

下面,我们来看一个Repeater控件实现分页的例子:

一、设计界面:

 

二、前台代码:

 

[html] view plain copy
  1. <form id="form1" runat="server">  
  2. <div align="center" style="height: 379px; width: 780px">  
  3.     <br />  
  4.     分 页 显 示:  
  5.     <br />  
  6.     <br />  
  7.     当前页:  
  8.     <asp:Label ID="Label1" runat="server" Text="Label">asp:Label>  
  9.     <br />  
  10.     <br />  
  11.     <asp:Button ID="btnBefore" runat="server" Text="上一页" Font-Bold="True" Font-Size="Medium"  
  12.         OnClick="btnBefore_Click" />  
  13.     <asp:Button ID="btnNext" runat="server" Text="下一页" Font-Bold="True" Font-Size="Medium"  
  14.         OnClick="btnNext_Click" />  
  15.     <br />  
  16.     <br />  
  17.    <%-- Panel容器--%>  
  18.     <asp:Panel ID="Panel1" runat="server" Height="180px" Width="441px">  
  19.         <%-- Repeater控件--%>  
  20.         <asp:Repeater ID="Repeater1" runat="server">  
  21.             <%--正常项--%>  
  22.             <ItemTemplate>  
  23.                 <%# DataBinder.Eval(Container.DataItem,"cityID") %> <%--绑定数据库字段--%>  
  24.                 <%# DataBinder.Eval(Container.DataItem,"cityName") %>  
  25.             ItemTemplate>  
  26.             <HeaderTemplate>  
  27.                 <hr size="1">  
  28.                 模版页眉  
  29.                 <hr color="blue" size="1">  
  30.             HeaderTemplate>  
  31.             <FooterTemplate>  
  32.                 <hr color="blue" size="1">  
  33.                 模版页脚  
  34.                 <hr size="1">  
  35.             FooterTemplate>  
  36.             <SeparatorTemplate>  
  37.                 <hr color="blue" size="1">  
  38.             SeparatorTemplate>  
  39.         asp:Repeater>  
  40.     asp:Panel>  
  41. div>  


三、创建数据库:

[sql] view plain copy
  1. create database city --创建数据库  
  2.   
  3. use city  
  4.   
  5. create table city --创建城市表  
  6. (  
  7.     cityID int primary key,  
  8.     proID int foreign key references province(proID),  
  9.     cityName varchar(50) not null  
  10. )  
  11.   
  12. insert into city values(1,1,'北京')  
  13. insert into city values(2,2,'长春')  
  14. insert into city values(3,2,'吉林')  
  15. insert into city values(4,3,'黑龙江')  
  16. insert into city values(5,3,'辽宁')  
  17. insert into city values(6,1,'内蒙古')  

 

四、后台代码:

    1、 创建一个DBCon类用于数据库连接:

[csharp] view plain copy
  1. ///   
  2. /// 数据库连接  
  3. ///   
  4. public class DBCon  
  5. {  
  6.     public static SqlConnection createCon()  
  7.     {  
  8.         SqlConnection con= new SqlConnection("server=.;DataBase=city;User ID=sa;Pwd=123456");  
  9.         return con;  
  10.     }  
  11. }  

 

    2、后台代码:

[csharp] view plain copy
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!Page.IsPostBack )  
  4.     {  
  5.         this.Label1.Text = "1";//设置起始页为第一页  
  6.         this.dataBindToRepeater();//绑定数据  
  7.     }  
  8. }  
  9. ///   
  10. /// 绑定  
  11. ///   
  12. private void dataBindToRepeater()  
  13. {  
  14.     int curPage =Convert.ToInt32( this.Label1.Text);  
  15.     SqlConnection con = DBCon.createCon();  
  16.     SqlDataAdapter sda = new SqlDataAdapter();  
  17.     sda.SelectCommand = new SqlCommand("select * from city", con);  
  18.     DataSet ds = new DataSet();  
  19.     sda.Fill(ds, "city");  
  20.     System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();  
  21.     ps.DataSource = ds.Tables["city"].DefaultView;//设置分页数据源的视图  
  22.     ps.AllowPaging = true//启用分页  
  23.     ps.PageSize = 2;//设置单页上显示的项数  
  24.     ps.CurrentPageIndex = curPage;//当前页的索引是从零开始的  
  25.   
  26.     //控制按钮的Enabled属性。  
  27.     this.btnBefore.Enabled = true;  
  28.     this.btnNext.Enabled = true;  
  29.     if (1 == curPage)  
  30.     {  
  31.         this.btnBefore.Enabled = false;  
  32.     }  
  33.     if (ps.PageCount == curPage)  
  34.     {  
  35.         this.btnNext.Enabled = false;  
  36.     }  
  37.     //绑定控件  
  38.     this.Repeater1.DataSource = ps ;  
  39.     this.Repeater1.DataBind();  
  40. }  
  41. ///   
  42. /// 下一步  
  43. ///   
  44. ///   
  45. ///   
  46. protected void btnBefore_Click(object sender, EventArgs e)  
  47. {  
  48.     this.Label1.Text=Convert.ToString( Convert.ToInt32( this.Label1.Text)-1);  
  49.     this.dataBindToRepeater();  
  50. }  
  51. ///   
  52. /// 上一步  
  53. ///   
  54. ///   
  55. ///   
  56. protected void btnNext_Click(object sender, EventArgs e)  
  57. {  
  58.     this.Label1.Text=Convert.ToString(Convert.ToInt32( this.Label1.Text)+1);  
  59.     this.dataBindToRepeater();  
  60. }  

 

五、显示结果:

 

转载于:https://www.cnblogs.com/liu765023051/archive/2012/12/13/2816934.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部