使用PagedDataSource 分页(Repearter 不能直接分页,给Repeater 改造分页显示)
Posted on 2006-10-25 00:40 孤峰皓月 阅读(1740) 评论(1) 编辑 收藏 举报
Repearter 不能直接分页,给Repeater 改造分页显示
前台页面代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContainerRepeartr2.aspx.cs" Inherits="test_DataBind_ContainerRepeartr2" %>
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 <asp:Panel ID="Panel1" runat="server" Height="440px" Width="798px">
13 <br />
14 Repearter 不能直接分页,给Repeater 改造分页显示<br />
15 <br />
16 <asp:Label ID="Label1" runat="server" Text="当前页码:"></asp:Label>
17 <asp:Label ID="Label2" runat="server"></asp:Label>
18 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" />
19 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" /><br />
20 <br />
21 <asp:Repeater ID="Repeater1" runat="server">
22 <AlternatingItemTemplate>
23 <font color="blue">
24 <%# DataBinder.Eval(Container.DataItem,"personname") %>
25 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
26 </font>
27 <br />
28 </AlternatingItemTemplate>
29 <FooterTemplate>
30 <h3>
31 页尾</h3>
32 </FooterTemplate>
33 <HeaderTemplate>
34 <h3>
35 页头</h3>
36 </HeaderTemplate>
37 <ItemTemplate>
38 <%# DataBinder.Eval(Container.DataItem,"personname") %>
39 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
40 <br />
41 </ItemTemplate>
42 <SeparatorTemplate>
43 <hr color="blue" size="1" />
44 </SeparatorTemplate>
45 </asp:Repeater>
46 </asp:Panel>
47
48 </div>
49 </form>
50</body>
51</html>
52
前台页面代码2,这个是对 Repeater 加了表格效果
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContainerRepeartr2.aspx.cs" Inherits="test_DataBind_ContainerRepeartr2" %>
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 <asp:Panel ID="Panel1" runat="server" Height="440px" Width="798px">
13 <br />
14 Repearter 不能直接分页,给Repeater 改造分页显示<br />
15 <br />
16 <asp:Label ID="Label1" runat="server" Text="当前页码:"></asp:Label>
17 <asp:Label ID="Label2" runat="server"></asp:Label>
18 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" />
19 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" /><br />
20 <br />
21 <asp:Repeater ID="Repeater1" runat="server">
22 <AlternatingItemTemplate>
23 <tr>
24 <td>
25 <font color="blue">
26 <%# DataBinder.Eval(Container.DataItem,"personname") %>
27 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
28 </font>
29 </td>
30 </tr>
31 </AlternatingItemTemplate>
32 <FooterTemplate>
33 </table>
34 </FooterTemplate>
35 <HeaderTemplate>
36 <table border="1">
37 <tr>
38 <td align=center>
39 页工名字
40 </td>
41 </tr>
42 </HeaderTemplate>
43 <ItemTemplate>
44 <tr>
45 <td>
46 <%# DataBinder.Eval(Container.DataItem,"personname") %>
47 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
48 </td>
49 </tr>
50 </ItemTemplate>
51 </asp:Repeater>
52 </asp:Panel>
53
54 </div>
55 </form>
56</body>
57</html>
58
注意区别于前台页面代码1
有些模板用了表格就去掉了,比如风格模板
在头和尾加<table>和</table>
在头加上<tr><td>标题:员工资料</td></tr>
在正常模板<ItemTemplate>和交叉模板<AlternatingItemTemplate>中加入<tr><td></td></tr>
后台页面代码:
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;
12public partial class test_DataBind_ContainerRepeartr2 : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!this.IsPostBack)
17 {
18 //页面第一次载入时设置页面起始页为第一页.
19 this.Label2.Text = "1";
20 this.fengye();
21 }
22 }
23
24 public void fengye()
25 {
26 int curPage = Convert.ToInt32(this.Label2.Text);
27 SqlConnection con = DBtest.createCon();
28 SqlDataAdapter sdr = new SqlDataAdapter();
29 sdr.SelectCommand = new SqlCommand("select * from person", con);
30 DataSet ds = new DataSet();
31 sdr.Fill(ds, "emp"); //用数据适配器填充数据集.
32 PagedDataSource ps = new PagedDataSource();
33 ps.DataSource = ds.Tables["emp"].DefaultView; //视图作为数据源.
34
35 //下面设置分页属性
36 ps.AllowPaging = true;
37 ps.PageSize = 3;
38 ps.CurrentPageIndex = curPage - 1; //起始页从0开始.
39
40 //每次绑定设置按钮为可用状态,
41 //再判断是否是第一页或最后一页,若是,则设置相应的按钮为不可用
42 this.Button1.Enabled = true;
43 this.Button2.Enabled = true;
44 if(curPage==1)
45 {
46 this.Button1.Enabled = false;
47 }
48 if(curPage==ps.PageCount)
49 {
50 this.Button2.Enabled = false;
51 }
52
53
54 this.Repeater1.DataSource = ps;//这里不再使用ds;改用ps了
55 this.Repeater1.DataBind();
56 }
57 protected void Button1_Click(object sender, EventArgs e)
58 {
59 //注意不要丢了 this.fengye();
60 //
61 this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text)-1);
62 this.fengye();
63 }
64 protected void Button2_Click(object sender, EventArgs e)
65 {
66 this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text) + 1);
67 this.fengye();
68 }
69}
70
明天将学习 DataList ,哈哈,应该说今天了,都快凌晨1点了,该睡觉类!!
前台页面代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContainerRepeartr2.aspx.cs" Inherits="test_DataBind_ContainerRepeartr2" %>
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 <asp:Panel ID="Panel1" runat="server" Height="440px" Width="798px">
13 <br />
14 Repearter 不能直接分页,给Repeater 改造分页显示<br />
15 <br />
16 <asp:Label ID="Label1" runat="server" Text="当前页码:"></asp:Label>
17 <asp:Label ID="Label2" runat="server"></asp:Label>
18 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" />
19 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" /><br />
20 <br />
21 <asp:Repeater ID="Repeater1" runat="server">
22 <AlternatingItemTemplate>
23 <font color="blue">
24 <%# DataBinder.Eval(Container.DataItem,"personname") %>
25 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
26 </font>
27 <br />
28 </AlternatingItemTemplate>
29 <FooterTemplate>
30 <h3>
31 页尾</h3>
32 </FooterTemplate>
33 <HeaderTemplate>
34 <h3>
35 页头</h3>
36 </HeaderTemplate>
37 <ItemTemplate>
38 <%# DataBinder.Eval(Container.DataItem,"personname") %>
39 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
40 <br />
41 </ItemTemplate>
42 <SeparatorTemplate>
43 <hr color="blue" size="1" />
44 </SeparatorTemplate>
45 </asp:Repeater>
46 </asp:Panel>
47
48 </div>
49 </form>
50</body>
51</html>
52
前台页面代码2,这个是对 Repeater 加了表格效果
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContainerRepeartr2.aspx.cs" Inherits="test_DataBind_ContainerRepeartr2" %>
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 <asp:Panel ID="Panel1" runat="server" Height="440px" Width="798px">
13 <br />
14 Repearter 不能直接分页,给Repeater 改造分页显示<br />
15 <br />
16 <asp:Label ID="Label1" runat="server" Text="当前页码:"></asp:Label>
17 <asp:Label ID="Label2" runat="server"></asp:Label>
18 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" />
19 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" /><br />
20 <br />
21 <asp:Repeater ID="Repeater1" runat="server">
22 <AlternatingItemTemplate>
23 <tr>
24 <td>
25 <font color="blue">
26 <%# DataBinder.Eval(Container.DataItem,"personname") %>
27 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
28 </font>
29 </td>
30 </tr>
31 </AlternatingItemTemplate>
32 <FooterTemplate>
33 </table>
34 </FooterTemplate>
35 <HeaderTemplate>
36 <table border="1">
37 <tr>
38 <td align=center>
39 页工名字
40 </td>
41 </tr>
42 </HeaderTemplate>
43 <ItemTemplate>
44 <tr>
45 <td>
46 <%# DataBinder.Eval(Container.DataItem,"personname") %>
47 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
48 </td>
49 </tr>
50 </ItemTemplate>
51 </asp:Repeater>
52 </asp:Panel>
53
54 </div>
55 </form>
56</body>
57</html>
58
注意区别于前台页面代码1
有些模板用了表格就去掉了,比如风格模板
在头和尾加<table>和</table>
在头加上<tr><td>标题:员工资料</td></tr>
在正常模板<ItemTemplate>和交叉模板<AlternatingItemTemplate>中加入<tr><td></td></tr>
后台页面代码:
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;
12public partial class test_DataBind_ContainerRepeartr2 : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!this.IsPostBack)
17 {
18 //页面第一次载入时设置页面起始页为第一页.
19 this.Label2.Text = "1";
20 this.fengye();
21 }
22 }
23
24 public void fengye()
25 {
26 int curPage = Convert.ToInt32(this.Label2.Text);
27 SqlConnection con = DBtest.createCon();
28 SqlDataAdapter sdr = new SqlDataAdapter();
29 sdr.SelectCommand = new SqlCommand("select * from person", con);
30 DataSet ds = new DataSet();
31 sdr.Fill(ds, "emp"); //用数据适配器填充数据集.
32 PagedDataSource ps = new PagedDataSource();
33 ps.DataSource = ds.Tables["emp"].DefaultView; //视图作为数据源.
34
35 //下面设置分页属性
36 ps.AllowPaging = true;
37 ps.PageSize = 3;
38 ps.CurrentPageIndex = curPage - 1; //起始页从0开始.
39
40 //每次绑定设置按钮为可用状态,
41 //再判断是否是第一页或最后一页,若是,则设置相应的按钮为不可用
42 this.Button1.Enabled = true;
43 this.Button2.Enabled = true;
44 if(curPage==1)
45 {
46 this.Button1.Enabled = false;
47 }
48 if(curPage==ps.PageCount)
49 {
50 this.Button2.Enabled = false;
51 }
52
53
54 this.Repeater1.DataSource = ps;//这里不再使用ds;改用ps了
55 this.Repeater1.DataBind();
56 }
57 protected void Button1_Click(object sender, EventArgs e)
58 {
59 //注意不要丢了 this.fengye();
60 //
61 this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text)-1);
62 this.fengye();
63 }
64 protected void Button2_Click(object sender, EventArgs e)
65 {
66 this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text) + 1);
67 this.fengye();
68 }
69}
70
明天将学习 DataList ,哈哈,应该说今天了,都快凌晨1点了,该睡觉类!!