前台:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeart2.aspx.cs" Inherits="repeart_repeart2" %>
2<%@ Import Namespace="System.Data" %>
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 Repeater 实现分页、自行绑定数据(不用数据源)<br />
13 对Repeater 进行镶嵌使用.<br />
14 <br />
15 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
16 <HeaderTemplate>
17 <table>
18 <tr>
19 <td>
20 作者
21 </td>
22 <td>
23 书籍
24 </td>
25 </tr>
26 </HeaderTemplate>
27 <ItemTemplate>
28 <tr>
29 <td>
30 <a href="repeart1.aspx"><%# Eval("au_lname") %></a>
31 </td>
32 <td>
33 <%--这里是第二个Repeater,放在第一个Repeater的 ItemTemplate 中。--%>
34 <asp:Repeater ID="Repeater2" runat="server" datasource='<%# Eval("myrela") %>'>
35 <ItemTemplate>
36 <%-- 注意这里的写法 --%>
37 <%# Eval("[\"title_id\"]")%><br>
38 </ItemTemplate>
39 </asp:Repeater>
40 </td>
41 </tr>
42 </ItemTemplate>
43 <SeparatorTemplate>
44 <tr>
45 <td colspan="2">
46 <hr size="1pt" />
47 </td>
48 </tr>
49 </SeparatorTemplate>
50 <FooterTemplate>
51 <tr>
52 <td colspan="2">
53 共 <asp:Label ID="lblpTotal" runat="server" Text="Label"></asp:Label> 页 当前为第 <asp:Label ID="lblpCurrent" runat="server" Text="Label"></asp:Label> 页
54 <asp:HyperLink ID="hlfirst" runat="server" Text="首页"></asp:HyperLink>
55 <asp:HyperLink ID="hlup" runat="server" Text="上一页"></asp:HyperLink>
56 <asp:HyperLink ID="hlnext" runat="server" Text="下一页"></asp:HyperLink>
57 <asp:HyperLink ID="hllast" runat="server" Text="末页"></asp:HyperLink>
58 </td>
59 </tr>
60 </table>
61 </FooterTemplate>
62 </asp:Repeater>
63
64 </div>
65 </form>
66</body>
67</html>
68
========================================================
后台
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13public partial class repeart_repeart2 : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!Page.IsPostBack)
18 {
19 this.Repeater1.DataSource = pds();
20 this.Repeater1.DataBind();
21 }
22 }
23
24 public PagedDataSource pds()
25 {
26 //string connstring = ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
27 ////声明一个字符串,后面随时可以用
28 //SqlConnection con = new SqlConnection(connstring);
29 ////初始化连接
30 //SqlDataAdapter sda = new SqlDataAdapter("select * from authors", con);
31 ////初始化一个SqlDataAdapter,并给出查询语句
32 //DataSet ds = new DataSet();
33 ////初始化一个DataSet
34 //sda.Fill(ds, "name");
35 ////将上面查询到的数据填充到name表中
36 //SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor", con);
37 ////同上
38 //sda2.Fill(ds, "title");
39 ////同上
40 //ds.Relations.Add("myrela", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);
41 ////为上面建立的两个表创建一个关系,指明父列和子列的名称并为他们的关系命名,前面将会用到
42
43
44 string connstring=ConfigurationManager.ConnectionStrings["pubs"].ConnectionString;
45 SqlConnection con = new SqlConnection(connstring);
46
47 SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
48 DataSet ds = new DataSet();
49 sda.Fill(ds,"name");
50
51 SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);
52 sda2.Fill(ds,"title");
53
54 ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);
55
56 //PagedDataSource pds = new PagedDataSource();
57 ////初始化一个PagedDataSource,允许控件分页
58 //pds.DataSource = ds.Tables["name"].DefaultView;
59 ////将上面的ds转换成标准数据视图
60 //pds.AllowPaging = true;
61 ////允许分页
62 //pds.PageSize = 5;
63 ////每页大小为5
64 //pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
65 ////设置当前页
66 //return pds;
67 ////将处理完毕的pds对象发出去
68
69 PagedDataSource pds = new PagedDataSource();
70 pds.DataSource = ds.Tables["name"].DefaultView;
71 pds.AllowPaging = true;
72 pds.PageSize = 5;
73 pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
74 return pds;
75 }
76 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
77 {
78 if(e.Item.ItemType==ListItemType.Footer)
79 {
80 //int n = pds().PageCount;//将分页总数赋给变量n
81 //int i = pds().CurrentPageIndex;//将当前分页码赋给i
82
83 //Label lblpc = (Label)e.Item.FindControl("lblpc");
84 //lblpc.Text = n.ToString();
85 ////找到lblpc这个Label,将总页码赋给他
86 //Label lblp = (Label)e.Item.FindControl("lblp");
87 //lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
88 ////找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
89 //HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
90 //hlfir.NavigateUrl = "?page=0";
91 //HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
92 //hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
93 ////找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
94 //HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
95 //HyperLink hln = (HyperLink)e.Item.FindControl("hln");
96
97 //ssssssssssssssssssss
98 int n = pds().PageCount; //分页总数
99 int i = pds().CurrentPageIndex;//当前页码
100 //下两行是总页数
101 Label lblpTotal = (Label)e.Item.FindControl("lblpTotal");
102 lblpTotal.Text = n.ToString();
103 //下两行是当前页码
104 Label lblpCurrent = (Label)e.Item.FindControl("lblpCurrent");
105 lblpCurrent.Text = Convert.ToString(pds().CurrentPageIndex + 1);//页码从0开始
106
107 //首页,尾页
108 HyperLink hlfirst = (HyperLink)e.Item.FindControl("hlfirst");
109 hlfirst.NavigateUrl = "?page=0";
110 HyperLink hllast = (HyperLink)e.Item.FindControl("hllast");
111 hllast.NavigateUrl = "?page=" + Convert.ToInt32(n-1);
112
113 //注意,这里先申明一下.
114 HyperLink hlup = (HyperLink)e.Item.FindControl("hlup");
115 HyperLink hlnext = (HyperLink)e.Item.FindControl("hlnext");
116
117 //找到表示上页和下页这两个控件
118 if (i <= 0)
119 {//如果当前页已经是第0页
120 hlup.Enabled = false;
121 hlfirst.Enabled = false;
122 hlnext.Enabled = true;
123 }
124 else
125 {
126 hlup.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
127 }
128 if (i > n - 2)
129 {//如果当前项已经是最末页
130 hlnext.Enabled = false;
131 hllast.Enabled = false;
132 hlup.Enabled = true;
133 }
134 else
135 {
136 hlnext.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
137 }
138
139
140 }
141 }
142}
143
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeart2.aspx.cs" Inherits="repeart_repeart2" %>
2<%@ Import Namespace="System.Data" %>
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 Repeater 实现分页、自行绑定数据(不用数据源)<br />
13 对Repeater 进行镶嵌使用.<br />
14 <br />
15 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
16 <HeaderTemplate>
17 <table>
18 <tr>
19 <td>
20 作者
21 </td>
22 <td>
23 书籍
24 </td>
25 </tr>
26 </HeaderTemplate>
27 <ItemTemplate>
28 <tr>
29 <td>
30 <a href="repeart1.aspx"><%# Eval("au_lname") %></a>
31 </td>
32 <td>
33 <%--这里是第二个Repeater,放在第一个Repeater的 ItemTemplate 中。--%>
34 <asp:Repeater ID="Repeater2" runat="server" datasource='<%# Eval("myrela") %>'>
35 <ItemTemplate>
36 <%-- 注意这里的写法 --%>
37 <%# Eval("[\"title_id\"]")%><br>
38 </ItemTemplate>
39 </asp:Repeater>
40 </td>
41 </tr>
42 </ItemTemplate>
43 <SeparatorTemplate>
44 <tr>
45 <td colspan="2">
46 <hr size="1pt" />
47 </td>
48 </tr>
49 </SeparatorTemplate>
50 <FooterTemplate>
51 <tr>
52 <td colspan="2">
53 共 <asp:Label ID="lblpTotal" runat="server" Text="Label"></asp:Label> 页 当前为第 <asp:Label ID="lblpCurrent" runat="server" Text="Label"></asp:Label> 页
54 <asp:HyperLink ID="hlfirst" runat="server" Text="首页"></asp:HyperLink>
55 <asp:HyperLink ID="hlup" runat="server" Text="上一页"></asp:HyperLink>
56 <asp:HyperLink ID="hlnext" runat="server" Text="下一页"></asp:HyperLink>
57 <asp:HyperLink ID="hllast" runat="server" Text="末页"></asp:HyperLink>
58 </td>
59 </tr>
60 </table>
61 </FooterTemplate>
62 </asp:Repeater>
63
64 </div>
65 </form>
66</body>
67</html>
68
========================================================
后台
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13public partial class repeart_repeart2 : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!Page.IsPostBack)
18 {
19 this.Repeater1.DataSource = pds();
20 this.Repeater1.DataBind();
21 }
22 }
23
24 public PagedDataSource pds()
25 {
26 //string connstring = ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
27 ////声明一个字符串,后面随时可以用
28 //SqlConnection con = new SqlConnection(connstring);
29 ////初始化连接
30 //SqlDataAdapter sda = new SqlDataAdapter("select * from authors", con);
31 ////初始化一个SqlDataAdapter,并给出查询语句
32 //DataSet ds = new DataSet();
33 ////初始化一个DataSet
34 //sda.Fill(ds, "name");
35 ////将上面查询到的数据填充到name表中
36 //SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor", con);
37 ////同上
38 //sda2.Fill(ds, "title");
39 ////同上
40 //ds.Relations.Add("myrela", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);
41 ////为上面建立的两个表创建一个关系,指明父列和子列的名称并为他们的关系命名,前面将会用到
42
43
44 string connstring=ConfigurationManager.ConnectionStrings["pubs"].ConnectionString;
45 SqlConnection con = new SqlConnection(connstring);
46
47 SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
48 DataSet ds = new DataSet();
49 sda.Fill(ds,"name");
50
51 SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);
52 sda2.Fill(ds,"title");
53
54 ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);
55
56 //PagedDataSource pds = new PagedDataSource();
57 ////初始化一个PagedDataSource,允许控件分页
58 //pds.DataSource = ds.Tables["name"].DefaultView;
59 ////将上面的ds转换成标准数据视图
60 //pds.AllowPaging = true;
61 ////允许分页
62 //pds.PageSize = 5;
63 ////每页大小为5
64 //pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
65 ////设置当前页
66 //return pds;
67 ////将处理完毕的pds对象发出去
68
69 PagedDataSource pds = new PagedDataSource();
70 pds.DataSource = ds.Tables["name"].DefaultView;
71 pds.AllowPaging = true;
72 pds.PageSize = 5;
73 pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
74 return pds;
75 }
76 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
77 {
78 if(e.Item.ItemType==ListItemType.Footer)
79 {
80 //int n = pds().PageCount;//将分页总数赋给变量n
81 //int i = pds().CurrentPageIndex;//将当前分页码赋给i
82
83 //Label lblpc = (Label)e.Item.FindControl("lblpc");
84 //lblpc.Text = n.ToString();
85 ////找到lblpc这个Label,将总页码赋给他
86 //Label lblp = (Label)e.Item.FindControl("lblp");
87 //lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
88 ////找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
89 //HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
90 //hlfir.NavigateUrl = "?page=0";
91 //HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
92 //hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
93 ////找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
94 //HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
95 //HyperLink hln = (HyperLink)e.Item.FindControl("hln");
96
97 //ssssssssssssssssssss
98 int n = pds().PageCount; //分页总数
99 int i = pds().CurrentPageIndex;//当前页码
100 //下两行是总页数
101 Label lblpTotal = (Label)e.Item.FindControl("lblpTotal");
102 lblpTotal.Text = n.ToString();
103 //下两行是当前页码
104 Label lblpCurrent = (Label)e.Item.FindControl("lblpCurrent");
105 lblpCurrent.Text = Convert.ToString(pds().CurrentPageIndex + 1);//页码从0开始
106
107 //首页,尾页
108 HyperLink hlfirst = (HyperLink)e.Item.FindControl("hlfirst");
109 hlfirst.NavigateUrl = "?page=0";
110 HyperLink hllast = (HyperLink)e.Item.FindControl("hllast");
111 hllast.NavigateUrl = "?page=" + Convert.ToInt32(n-1);
112
113 //注意,这里先申明一下.
114 HyperLink hlup = (HyperLink)e.Item.FindControl("hlup");
115 HyperLink hlnext = (HyperLink)e.Item.FindControl("hlnext");
116
117 //找到表示上页和下页这两个控件
118 if (i <= 0)
119 {//如果当前页已经是第0页
120 hlup.Enabled = false;
121 hlfirst.Enabled = false;
122 hlnext.Enabled = true;
123 }
124 else
125 {
126 hlup.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
127 }
128 if (i > n - 2)
129 {//如果当前项已经是最末页
130 hlnext.Enabled = false;
131 hllast.Enabled = false;
132 hlup.Enabled = true;
133 }
134 else
135 {
136 hlnext.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
137 }
138
139
140 }
141 }
142}
143