Repeater分页

第一种方式:

数据库连接代码:

view source pr
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Web;
05 using System.Web.Security;
06 using System.Web.UI;
07 using System.Web.UI.WebControls;
08 using System.Web.UI.WebControls.WebParts;
09 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using System.Data.Sql;
12  
13  
14  
15 public partial class _Default : System.Web.UI.Page
16 {
17 private void con()
18 {
19 string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
20 SqlConnection con = new SqlConnection(connstring);
21 SqlConnection conn = new SqlConnection();
22 DataSet ds = new DataSet();
23 SqlDataAdapter sda = new SqlDataAdapter("select * from FactSalesQuota", con);
24 sda.Fill(ds, "name");
25 SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
26 sda2.Fill(ds, "title");
27 PagedDataSource pds = new PagedDataSource();
28 pds.DataSource = ds.Tables["name"].DefaultView;
29 //PagedDataSource aa = new PagedDataSource();
30 pds.AllowPaging = true;//允许分页
31 pds.PageSize = 8;//单页显示项数
32 int CurPage;
33 if (Request.QueryString["Page"] != null)
34 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
35 else
36 CurPage = 1;
37 pds.CurrentPageIndex = CurPage - 1;
38 int Count = pds.PageCount;
39  
40 lblCurrentPage.Text = "当前页:" + CurPage.ToString();
41 labPage.Text = Count.ToString();
42  
43 if (!pds.IsFirstPage)
44 {
45 this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
46 this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count - 1); ;
47 up.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
48 }
49 else
50 {
51 this.first.Visible = false ;
52 this.last.Visible = false ;
53  
54 }
55  
56 if (!pds.IsLastPage)
57 {
58  
59  
60 next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
61 }
62 else
63 {
64 this.first.Visible = false;
65 this.last.Visible = false;
66  
67 }
68  
69 Repeater1.DataSource = pds ;
70 Repeater1.DataBind();
71  
72 }
73  
74  
75  
76 protected void Page_Load(object sender, EventArgs e)
77 {
78 if (!IsPostBack)
79 {
80 con();
81 this.first.Visible = true;
82 this.last.Visible = true;
83 //this.Repeater1.DataSource = pds();
84 //this.Repeater1.DataBind();
85  
86 }
87  
88 }
89 }

 

aspx文件代码:

view source print ?
01
02
class="style1" align ="left" >hehe
03  
04
class="style1">
05 "Repeater1" runat="server" >
06  
07
头模板
08
"red" > <%#Eval("timekey")%>
10
尾模板
11
12
13  
14  
15
class="style1">
16 "first" runat="server">首页
17 "next" runat="server">下一页
18 "up" runat="server">上一页
19 "last" runat="server">末页
20
21  
22
class="style1">当前页为:"lblCurrentPage" runat="server"
23 Text="Label">
24
25 "labPage" runat="server" Text="Label">
26
27  
28

第二种方式:

view source print ?
01 using System;
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Data.SqlClient;
14  
15 public partial class databind : System.Web.UI.Page
16 {
17  
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 if (!IsPostBack)
21 {
22 num.Text = "1";
23 repdatabind();
24 }
25  
26  
27 }
28 public void repdatabind()
29 {
30 string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
31 SqlConnection con = new SqlConnection(connstring);
32 SqlConnection conn = new SqlConnection();
33 DataSet ds = new DataSet();
34 SqlDataAdapter sda = new SqlDataAdapter("select * from DimProduct", con);
35 sda.Fill(ds, "name");
36 PagedDataSource pds = new PagedDataSource();
37 pds.DataSource = ds.Tables["name"].DefaultView;
38 pds.AllowPaging = true;//允许分页
39 pds.PageSize = 8;//单页显示项数
40  
41 int curpage = Convert.ToInt32(num.Text);
42 this.BtnDown.Enabled = true;
43 this.BtnUp.Enabled = true;
44 pds.CurrentPageIndex = curpage - 1;
45 if (curpage == 1)
46 {
47 this.BtnUp.Enabled = false;
48 }
49 if (curpage == pds.PageCount)
50 {
51 this.BtnDown.Enabled = false;
52 }
53 this.Repeater1.DataSource = pds;
54 this.Repeater1.DataBind();
55 }
56  
57 protected void BtnUp_Click(object sender, EventArgs e)
58 {
59 this.num.Text =Convert.ToString ( Convert.ToInt32(num.Text)- 1) ;
60 repdatabind();
61 }
62 protected void BtnDown_Click(object sender, EventArgs e)
63 {
64 this.num.Text = Convert.ToString(Convert.ToInt32(num.Text)+ 1) ;
65 repdatabind();
66 }
67 }

aspx代码:

view source print ?
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="databind.aspx.cs" Inherits="databind" %>
02  
03 "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04  
05 "http://www.w3.org/1999/xhtml">
06 "server">
07 无标题页
08
09
10
"form1" runat="server">
11
12  
13 "Panel1" runat="server" Height="173px">
14   "Repeater1" runat="server">"1">
头模板
序号:<%# Eval("ProductKey") %>
编码:<%# Eval("ProductAlternateKey") %>
脚模板
< /FooterTemplate>
15
16 当前页:"num" runat="server">
17
18 "BtnUp" runat="server" onclick="BtnUp_Click" Text="上一页" />
19 "BtnDown" runat="server" onclick="BtnDown_Click" Text="下一页" />
20
21
22
23  
24
25
26
27

转载于:https://www.cnblogs.com/huangyangblogs/archive/2013/04/29/3050244.html

你可能感兴趣的:(Repeater分页)