ASP.NET 数据列表控件的分页总结(一)-----自定义方法分页和PageDataSource类分页(转)
在Asp.net中,提供了三个功能强大的列表控件:GridView、 DataList和Repeater控件,但其中只有GridView控件提供分页功能。虽然DataGrid提供了分页功能,不过看上去功能有限,但是 我们可以通过GridView的一些属性来获取状态以及增加首页、尾页功能按钮。如果在速度效率不是很讲究的情况下,由DataGrid自己管理分页还是 不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。所以若需要追求执行效率,而且数据量 比较大的情况下建议使用GridView的自定义分页功能。若数据量不是很大,需要追求更多的页面功能和样式,那么相对GridView来 说,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据。现在 我采用手动分页,对这三个控件作一比较。如下:
(1).使用GridView手动分页。
通过下拉框来控制分页。
前台代码:
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:GridView ID="gvShow" runat="server" OnPageIndexChanging="gvShow_PageIndexChanging" AllowPaging="True" PageSize="2">
5 </asp:GridView>
6 <br />
7 </div>
8 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
9 </asp:DropDownList>
10 </form>
11</body>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection con;
static SqlDataAdapter sda;
static SqlCommand cmd;
static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
//记住这句不能丢,否则行号会累加在下拉框中
DropDownList1.Items.Clear();
for (int i = 1; i< gvShow.PageCount; i++)
{
DropDownList1.Items.Add(i.ToString());
}
}
}
private void Bind()
{
//gvShow.AllowPaging = true;
//gvShow.PageSize = 2;
gvShow.DataSource =BindData();
gvShow.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//关键代码
gvShow.PageIndex = Convert.ToInt32(DropDownList1.SelectedValue)-1;
Bind();
}
protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvShow.PageIndex = e.NewPageIndex;
Bind();
}
private static void Init()
{
//数据库连接语句
con = new SqlConnection("Data Source=.;database=数据库名;uid=用户ID;pwd=用户密码");
ds = new DataSet();
try
{
con.Open();
}
catch
{
throw new Exception();
}
}
public static DataSet BindData()
{
Init();
string sql = "select * from book";
try
{
sda = new SqlDataAdapter(sql, con);
sda.Fill(ds, "book");
return ds;
}
catch
{
throw new Exception();
}
}
}
(2).使用DataList手动分页。
通过下拉框来控制分页。
前台代码:
1<body>
2<form id="form1" runat="server">
3 <table>
4 <tr>
5 <td align="center" style="width: 715px; height: 147px;">
6 <asp:datalist id="MyDataGrid" RepeatDirection="Horizontal" RepeatColumns="5" showfooter="true" borderwidth="0" runat="server">
7 <HeaderTemplate>
8 <table width="784" cellpadding="0" cellspacing="0" border="0" >
9 <tr>
10 </HeaderTemplate>
11 <FooterTemplate>
12 </tr>
13 </table>
14 </FooterTemplate>
15 <ItemTemplate>
16 <td align=center>
17 <table width="100%" height="120">
18 <tr>
19 <td width=154 align="center">
20 <img width=130 height=90 src='<%# DataBinder.Eval(Container.DataItem, "CustomerID", "Img.aspx?id={0}")%>'>
21 </td>
22 </tr>
23 <tr>
24 <td align=center>
25 名称: <%# DataBinder.Eval(Container.DataItem, "CompanyName")%>
26 </td>
27 </tr>
28 </table>
29 </td>
30 </ItemTemplate>
31 </asp:datalist>
32 </td>
33 </tr>
34 </table>
35 <br />
36 <br />
37 <br />
38 <br />
39 <tr align="center" valign="middle" width=100%>
40 <td align="center" valign="middle">
41
42 <asp:LinkButton ID="btnFrist" runat="server" CommandName="Pager" CommandArgument="Frist" OnCommand="PagerButtonClick" Text="首页"></asp:LinkButton></td>
43 <td align="center" valign="middle"><asp:LinkButton ID="btnPrev" runat="server" CommandName="Pager" CommandArgument="Prev" OnCommand="PagerButtonClick" Text="上一页"></asp:LinkButton></td>
44 <td align="center" valign="middle"><asp:LinkButton ID="btnNext" runat="server" CommandName="Pager" CommandArgument="Next" OnCommand="PagerButtonClick" Text="下一页"></asp:LinkButton></td>
45 <td align="center" valign="middle"><asp:LinkButton ID="btnLast" runat="server" CommandName="Pager" CommandArgument="Last" OnCommand="PagerButtonClick" Text="尾页"></asp:LinkButton></td>
46 <br />
47 <br />
48 <br />
49 <td align="right"valign="middle">页<asp:Label ID="lblCurrentPage" runat="server" /></td><td align="left" valign="middle">/<asp:Label ID="lalPageCount" runat="server"/></td>
50 <td align="center" valign="middle">共<asp:Label ID="lblRecordCount" runat="server" />条记录</td>
51 <td align="center" valign="middle">转到第<asp:DropDownList ID="D1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="D1_SelectedIndexChanged" />页</td>
52</form>
53</body>
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12
13public partial class _Default : System.Web.UI.Page
14{
15 private static int PageIndex;//保存当前页的索引
16 private static int pagesize;//保存每个页面中的记录数目
17 private static int CurrentPage;//为当前页序号-1
18 private static int PageCount;//保存总页数
19 private static int RecordCount;//总记录数
20
21 public bool DropFill;
22
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 if (!IsPostBack)
26 {
27 DropFill = true;
28 pagesize = 10;//pagesize = MyDataGrid.RepeatColumns;可以设DataList的属性RepeatDirection="Horizontal" RepeatColumns="10",通过RepeatColumns来设置每页显示的条数
29 CurrentPage = 0;
30 GetPageCount();//得到总记录数
31 Databind();
32 }
33
34 }
35 public void Databind()
36 {
37 D1.Items.Clear();
38 string sql = "select * from Customers";
39
40 SqlConnection con=new SqlConnection ("server=.;database=Northwind;uid=sa");
41 SqlCommand cmd = new SqlCommand(sql, con);
42
43 SqlDataAdapter da = new SqlDataAdapter(cmd);
44 DataSet ds = new DataSet();
45 PageIndex = CurrentPage * pagesize;
46 da.Fill(ds, PageIndex, pagesize, "table");
47
48 MyDataGrid.DataSource = ds.Tables["table"];
49 MyDataGrid.DataBind();
50 con.Close();
51 PageCount = RecordCount / pagesize;
52
53 if (RecordCount % pagesize != 0)
54 PageCount++;
55 lalPageCount.Text = PageCount.ToString();
56 lblRecordCount.Text = RecordCount.ToString();
57
58 if (lblCurrentPage.Text == "")
59 {
60 lblCurrentPage.Text = "1";
61 }
62
63 if (CurrentPage == 0)
64 {
65 btnFrist.Enabled = false;
66 btnPrev.Enabled = false;
67 }
68 if (CurrentPage == PageCount - 1)
69 {
70 btnLast.Enabled = false;
71 btnNext.Enabled = false;
72 }
73
74 //给下拉链表中添加页数
75 if (DropFill == true)
76 {
77 for (int i = 1; i <= PageCount; i++)
78 {
79 D1.Items.Add(new ListItem(i.ToString()));
80 }
81 }
82 else
83 {
84 D1.Items.Add("1");
85 }
86
87 }
88 public void GetPageCount()
89 {
90 string sql = "select * from Customers ";
91
92 SqlConnection c = new SqlConnection("server=.;database=Northwind;uid=sa");
93 SqlCommand cm = new SqlCommand(sql, c);
94
95 SqlDataAdapter da = new SqlDataAdapter(cm);
96 DataSet ds = new DataSet();
97 da.Fill(ds, "table");
98
99 RecordCount = ds.Tables["table"].DefaultView.Count;
100 c.Close();
101 }
102 protected void D1_SelectedIndexChanged(object sender, EventArgs e)
103 {
104 btnFrist.Enabled = true;
105 btnLast.Enabled = true;
106 btnNext.Enabled = true;
107 btnPrev.Enabled = true;
108 //DropFill = false;
109
110 CurrentPage = Int32.Parse(D1.SelectedValue.ToString()) - 1;//当前页
111 DropFill = true;
112 Databind();
113 lblCurrentPage.Text = (CurrentPage + 1).ToString();
114
115 }
116
117 protected void PagerButtonClick(object sender, CommandEventArgs e)
118 {
119 btnFrist.Enabled = true;
120 btnLast.Enabled = true;
121 btnNext.Enabled = true;
122 btnPrev.Enabled = true;
123 DropFill = true;
124
125 string age = e.CommandArgument.ToString();
126 switch (age)
127 {
128 case "Next"://后一页
129 if (CurrentPage < PageCount - 1)
130 {
131 CurrentPage++;
132 }
133 break;
134 case "Prev"://前一页
135 if (CurrentPage > 0)
136 {
137 CurrentPage--;
138 }
139 break;
140 case "Frist"://首页
141 {
142 CurrentPage = 0;
143 }
144 break;
145 case "Last"://尾页
146 {
147 CurrentPage = PageCount - 1;
148 }
149 break;
150 }
151 Databind();
152 lblCurrentPage.Text = Convert.ToString(CurrentPage + 1);
153 }
154}
155
(3).使用Repeater手动分页。
前台代码:
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:Repeater id="Repeater1" runat="server">
5 <ItemTemplate>
6 <table border="2">
7 <tr>
8 <td>电话号码:</td>
9 </td><td><%# Eval("phone") %></td>
10 <td>地址:</td>
11 </td><td><%# Eval("address") %></td>
12 </tr>
13 </table>
14 </ItemTemplate>
15 </asp:Repeater>
16 <asp:Label id="lblCurrentPage" style="Z-INDEX: 101; LEFT: 335px; POSITION: absolute; TOP: 207px"
17 runat="server"></asp:Label>
18 <asp:HyperLink id="lnkPrev" style="Z-INDEX: 102; LEFT: 465px; POSITION: absolute; TOP: 206px" runat="server" Width="32px"><</asp:HyperLink>
19 <asp:HyperLink id="lnkNext" style="Z-INDEX: 103; LEFT: 520px; POSITION: absolute; TOP: 206px" runat="server" Width="32px">></asp:HyperLink>
20 <asp:Label id="lblCount" style="Z-INDEX: 104; LEFT: 245px; POSITION: absolute; TOP: 207px" runat="server"></asp:Label>
21
22 <br />
23 </div>
24 </form>
25</body>
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11public partial class _Default : System.Web.UI.Page
12{
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
16 SqlDataAdapter sda = new SqlDataAdapter("select * from authors", Conn);
17 DataSet ds = new DataSet();
18 sda.Fill(ds, "users");
19
20 //对PagedDataSource 对象的相关属性赋值
21 PagedDataSource pds = new PagedDataSource();
22 //数据源指向定义好的DataSet
23 pds.DataSource = ds.Tables["users"].DefaultView;
24 //AllowPaging属性设置是否允许分页
25 pds.AllowPaging = true;
26 //设置每页条数
27 pds.PageSize = 2;
28 int CurPage;
29
30 //当前页面从Page查询参数获取
31 if (Request.QueryString["Page"] != null)
32 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
33 else
34 CurPage = 1;
35
36 pds.CurrentPageIndex = CurPage - 1;
37 //显示当前页数
38 lblCurrentPage.Text = "当前是第: " + CurPage.ToString() + "页";
39 //显示共有的页数
40 lblCount.Text = "共有" + Convert.ToString(pds.PageCount) + "页";
41
42 //向前翻页
43 if (!pds.IsFirstPage)
44 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
45
46 //向后翻页
47 if (!pds.IsLastPage)
48 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
49
50 //把PagedDataSource 对象赋给Repeater控件
51 Repeater1.DataSource = pds;
52 Repeater1.DataBind();
53 }
54}
55
总 结:上面前2种是采用DropDownList自定义一个方法的简单分页,最后一个是采用PagedDataSource类来分页。采用 PagedDataSource类,效率要低些,每次都要把所有页的数据都select出来,若用一个方法或者存储过程的话,仅仅只select出当前页 的数据,效率上就高些。至于对控件的使用,若需要追求执行效率,而且数据量比较大的情况下建议使用GridView的自定义分页功能。若数据量不是很大, 需要追求更多的页面功能和样式,那么可以考虑使用DataList和Repeater控件。
(1).使用GridView手动分页。
通过下拉框来控制分页。
前台代码:
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:GridView ID="gvShow" runat="server" OnPageIndexChanging="gvShow_PageIndexChanging" AllowPaging="True" PageSize="2">
5 </asp:GridView>
6 <br />
7 </div>
8 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
9 </asp:DropDownList>
10 </form>
11</body>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection con;
static SqlDataAdapter sda;
static SqlCommand cmd;
static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
//记住这句不能丢,否则行号会累加在下拉框中
DropDownList1.Items.Clear();
for (int i = 1; i< gvShow.PageCount; i++)
{
DropDownList1.Items.Add(i.ToString());
}
}
}
private void Bind()
{
//gvShow.AllowPaging = true;
//gvShow.PageSize = 2;
gvShow.DataSource =BindData();
gvShow.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//关键代码
gvShow.PageIndex = Convert.ToInt32(DropDownList1.SelectedValue)-1;
Bind();
}
protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvShow.PageIndex = e.NewPageIndex;
Bind();
}
private static void Init()
{
//数据库连接语句
con = new SqlConnection("Data Source=.;database=数据库名;uid=用户ID;pwd=用户密码");
ds = new DataSet();
try
{
con.Open();
}
catch
{
throw new Exception();
}
}
public static DataSet BindData()
{
Init();
string sql = "select * from book";
try
{
sda = new SqlDataAdapter(sql, con);
sda.Fill(ds, "book");
return ds;
}
catch
{
throw new Exception();
}
}
}
(2).使用DataList手动分页。
通过下拉框来控制分页。
前台代码:
1<body>
2<form id="form1" runat="server">
3 <table>
4 <tr>
5 <td align="center" style="width: 715px; height: 147px;">
6 <asp:datalist id="MyDataGrid" RepeatDirection="Horizontal" RepeatColumns="5" showfooter="true" borderwidth="0" runat="server">
7 <HeaderTemplate>
8 <table width="784" cellpadding="0" cellspacing="0" border="0" >
9 <tr>
10 </HeaderTemplate>
11 <FooterTemplate>
12 </tr>
13 </table>
14 </FooterTemplate>
15 <ItemTemplate>
16 <td align=center>
17 <table width="100%" height="120">
18 <tr>
19 <td width=154 align="center">
20 <img width=130 height=90 src='<%# DataBinder.Eval(Container.DataItem, "CustomerID", "Img.aspx?id={0}")%>'>
21 </td>
22 </tr>
23 <tr>
24 <td align=center>
25 名称: <%# DataBinder.Eval(Container.DataItem, "CompanyName")%>
26 </td>
27 </tr>
28 </table>
29 </td>
30 </ItemTemplate>
31 </asp:datalist>
32 </td>
33 </tr>
34 </table>
35 <br />
36 <br />
37 <br />
38 <br />
39 <tr align="center" valign="middle" width=100%>
40 <td align="center" valign="middle">
41
42 <asp:LinkButton ID="btnFrist" runat="server" CommandName="Pager" CommandArgument="Frist" OnCommand="PagerButtonClick" Text="首页"></asp:LinkButton></td>
43 <td align="center" valign="middle"><asp:LinkButton ID="btnPrev" runat="server" CommandName="Pager" CommandArgument="Prev" OnCommand="PagerButtonClick" Text="上一页"></asp:LinkButton></td>
44 <td align="center" valign="middle"><asp:LinkButton ID="btnNext" runat="server" CommandName="Pager" CommandArgument="Next" OnCommand="PagerButtonClick" Text="下一页"></asp:LinkButton></td>
45 <td align="center" valign="middle"><asp:LinkButton ID="btnLast" runat="server" CommandName="Pager" CommandArgument="Last" OnCommand="PagerButtonClick" Text="尾页"></asp:LinkButton></td>
46 <br />
47 <br />
48 <br />
49 <td align="right"valign="middle">页<asp:Label ID="lblCurrentPage" runat="server" /></td><td align="left" valign="middle">/<asp:Label ID="lalPageCount" runat="server"/></td>
50 <td align="center" valign="middle">共<asp:Label ID="lblRecordCount" runat="server" />条记录</td>
51 <td align="center" valign="middle">转到第<asp:DropDownList ID="D1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="D1_SelectedIndexChanged" />页</td>
52</form>
53</body>
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12
13public partial class _Default : System.Web.UI.Page
14{
15 private static int PageIndex;//保存当前页的索引
16 private static int pagesize;//保存每个页面中的记录数目
17 private static int CurrentPage;//为当前页序号-1
18 private static int PageCount;//保存总页数
19 private static int RecordCount;//总记录数
20
21 public bool DropFill;
22
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 if (!IsPostBack)
26 {
27 DropFill = true;
28 pagesize = 10;//pagesize = MyDataGrid.RepeatColumns;可以设DataList的属性RepeatDirection="Horizontal" RepeatColumns="10",通过RepeatColumns来设置每页显示的条数
29 CurrentPage = 0;
30 GetPageCount();//得到总记录数
31 Databind();
32 }
33
34 }
35 public void Databind()
36 {
37 D1.Items.Clear();
38 string sql = "select * from Customers";
39
40 SqlConnection con=new SqlConnection ("server=.;database=Northwind;uid=sa");
41 SqlCommand cmd = new SqlCommand(sql, con);
42
43 SqlDataAdapter da = new SqlDataAdapter(cmd);
44 DataSet ds = new DataSet();
45 PageIndex = CurrentPage * pagesize;
46 da.Fill(ds, PageIndex, pagesize, "table");
47
48 MyDataGrid.DataSource = ds.Tables["table"];
49 MyDataGrid.DataBind();
50 con.Close();
51 PageCount = RecordCount / pagesize;
52
53 if (RecordCount % pagesize != 0)
54 PageCount++;
55 lalPageCount.Text = PageCount.ToString();
56 lblRecordCount.Text = RecordCount.ToString();
57
58 if (lblCurrentPage.Text == "")
59 {
60 lblCurrentPage.Text = "1";
61 }
62
63 if (CurrentPage == 0)
64 {
65 btnFrist.Enabled = false;
66 btnPrev.Enabled = false;
67 }
68 if (CurrentPage == PageCount - 1)
69 {
70 btnLast.Enabled = false;
71 btnNext.Enabled = false;
72 }
73
74 //给下拉链表中添加页数
75 if (DropFill == true)
76 {
77 for (int i = 1; i <= PageCount; i++)
78 {
79 D1.Items.Add(new ListItem(i.ToString()));
80 }
81 }
82 else
83 {
84 D1.Items.Add("1");
85 }
86
87 }
88 public void GetPageCount()
89 {
90 string sql = "select * from Customers ";
91
92 SqlConnection c = new SqlConnection("server=.;database=Northwind;uid=sa");
93 SqlCommand cm = new SqlCommand(sql, c);
94
95 SqlDataAdapter da = new SqlDataAdapter(cm);
96 DataSet ds = new DataSet();
97 da.Fill(ds, "table");
98
99 RecordCount = ds.Tables["table"].DefaultView.Count;
100 c.Close();
101 }
102 protected void D1_SelectedIndexChanged(object sender, EventArgs e)
103 {
104 btnFrist.Enabled = true;
105 btnLast.Enabled = true;
106 btnNext.Enabled = true;
107 btnPrev.Enabled = true;
108 //DropFill = false;
109
110 CurrentPage = Int32.Parse(D1.SelectedValue.ToString()) - 1;//当前页
111 DropFill = true;
112 Databind();
113 lblCurrentPage.Text = (CurrentPage + 1).ToString();
114
115 }
116
117 protected void PagerButtonClick(object sender, CommandEventArgs e)
118 {
119 btnFrist.Enabled = true;
120 btnLast.Enabled = true;
121 btnNext.Enabled = true;
122 btnPrev.Enabled = true;
123 DropFill = true;
124
125 string age = e.CommandArgument.ToString();
126 switch (age)
127 {
128 case "Next"://后一页
129 if (CurrentPage < PageCount - 1)
130 {
131 CurrentPage++;
132 }
133 break;
134 case "Prev"://前一页
135 if (CurrentPage > 0)
136 {
137 CurrentPage--;
138 }
139 break;
140 case "Frist"://首页
141 {
142 CurrentPage = 0;
143 }
144 break;
145 case "Last"://尾页
146 {
147 CurrentPage = PageCount - 1;
148 }
149 break;
150 }
151 Databind();
152 lblCurrentPage.Text = Convert.ToString(CurrentPage + 1);
153 }
154}
155
(3).使用Repeater手动分页。
前台代码:
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:Repeater id="Repeater1" runat="server">
5 <ItemTemplate>
6 <table border="2">
7 <tr>
8 <td>电话号码:</td>
9 </td><td><%# Eval("phone") %></td>
10 <td>地址:</td>
11 </td><td><%# Eval("address") %></td>
12 </tr>
13 </table>
14 </ItemTemplate>
15 </asp:Repeater>
16 <asp:Label id="lblCurrentPage" style="Z-INDEX: 101; LEFT: 335px; POSITION: absolute; TOP: 207px"
17 runat="server"></asp:Label>
18 <asp:HyperLink id="lnkPrev" style="Z-INDEX: 102; LEFT: 465px; POSITION: absolute; TOP: 206px" runat="server" Width="32px"><</asp:HyperLink>
19 <asp:HyperLink id="lnkNext" style="Z-INDEX: 103; LEFT: 520px; POSITION: absolute; TOP: 206px" runat="server" Width="32px">></asp:HyperLink>
20 <asp:Label id="lblCount" style="Z-INDEX: 104; LEFT: 245px; POSITION: absolute; TOP: 207px" runat="server"></asp:Label>
21
22 <br />
23 </div>
24 </form>
25</body>
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11public partial class _Default : System.Web.UI.Page
12{
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
16 SqlDataAdapter sda = new SqlDataAdapter("select * from authors", Conn);
17 DataSet ds = new DataSet();
18 sda.Fill(ds, "users");
19
20 //对PagedDataSource 对象的相关属性赋值
21 PagedDataSource pds = new PagedDataSource();
22 //数据源指向定义好的DataSet
23 pds.DataSource = ds.Tables["users"].DefaultView;
24 //AllowPaging属性设置是否允许分页
25 pds.AllowPaging = true;
26 //设置每页条数
27 pds.PageSize = 2;
28 int CurPage;
29
30 //当前页面从Page查询参数获取
31 if (Request.QueryString["Page"] != null)
32 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
33 else
34 CurPage = 1;
35
36 pds.CurrentPageIndex = CurPage - 1;
37 //显示当前页数
38 lblCurrentPage.Text = "当前是第: " + CurPage.ToString() + "页";
39 //显示共有的页数
40 lblCount.Text = "共有" + Convert.ToString(pds.PageCount) + "页";
41
42 //向前翻页
43 if (!pds.IsFirstPage)
44 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
45
46 //向后翻页
47 if (!pds.IsLastPage)
48 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
49
50 //把PagedDataSource 对象赋给Repeater控件
51 Repeater1.DataSource = pds;
52 Repeater1.DataBind();
53 }
54}
55
总 结:上面前2种是采用DropDownList自定义一个方法的简单分页,最后一个是采用PagedDataSource类来分页。采用 PagedDataSource类,效率要低些,每次都要把所有页的数据都select出来,若用一个方法或者存储过程的话,仅仅只select出当前页 的数据,效率上就高些。至于对控件的使用,若需要追求执行效率,而且数据量比较大的情况下建议使用GridView的自定义分页功能。若数据量不是很大, 需要追求更多的页面功能和样式,那么可以考虑使用DataList和Repeater控件。