先写个Pages.cs类 最好放在
App_Code里

Code
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
11
12
/**//// <summary>
13
/// Page 的摘要说明
14
/// </summary>
15
public class Pages
16

{
17
//获得每页记录数
18
19
static int pageSize =8;//每页记录数
20
public Pages()
21
{
22
// TODO: 在此处添加构造函数逻辑
23
}
24
25
/**//***********************************************************************************************
26
* 功能 :DataGrid 分页属性设置
27
*
28
* dg :DataGrid控件名称
29
* *********************************************************************************************/
30
public static void dataGridProperty(DataGrid dg)
31
{
32
dg.AllowPaging = true;
33
dg.PagerStyle.Visible = false;
34
dg.PageSize = pageSize;
35
}
36
37
public static void gridViewProperty(GridView dg)
38
{
39
dg.AllowPaging = true;
40
//dg.PagerStyle.IsEmpty = false;
41
42
dg.PageSize = pageSize;
43
}
44
45
/**//***********************************************************************************************
46
* 功能 :用于显示"第几页,总*页"
47
*
48
* dg :DataGrid控件名称
49
* lblCurrentIndex :显示当前页的Label控件名称
50
* lblPageCount :显示总页数的Label控件名称
51
*
52
* *********************************************************************************************/
53
public static void ShowStats(Label lblCurrentIndex, Label lblPageCount, DataGrid dg)
54
{
55
lblCurrentIndex.Text = "第" + (dg.CurrentPageIndex + 1).ToString() + "页";
56
lblPageCount.Text = "共" + dg.PageCount.ToString() + "页";
57
}
58
59
60
public static void ShowStatsGrive(Label lblCurrentIndex, Label lblPageCount, GridView dg)
61
{
62
lblCurrentIndex.Text = "第" + (dg.PageIndex + 1).ToString() + "页";
63
lblPageCount.Text = "共" + dg.PageCount.ToString() + "页";
64
}
65
/**//**
66
/***********************************************************************************************
67
* 功能 :响应分页按钮
68
*
69
* sender :事件对象
70
* e :
71
* dg :DataGrid控件名称
72
* lblCurrentIndex :显示当前页的Label控件名称
73
* lblPageCount :显示总页数的Label控件名称
74
*
75
* *********************************************************************************************/
76
public static void PagerButtonClick(object sender, EventArgs e, DataGrid dg, Label lblCurrentIndex, Label lblPageCount)
77
{
78
string arg = ((LinkButton)sender).CommandArgument.ToString();
79
switch (arg)
80
{
81
case "next":
82
if (dg.CurrentPageIndex < (dg.PageCount - 1))
83
{
84
dg.CurrentPageIndex += 1;
85
}
86
break;
87
case "prev":
88
if (dg.CurrentPageIndex > 0)
89
{
90
dg.CurrentPageIndex -= 1;
91
}
92
break;
93
case "last":
94
dg.CurrentPageIndex = (dg.PageCount - 1);
95
break;
96
default:
97
dg.CurrentPageIndex = System.Convert.ToInt32(arg);
98
break;
99
}
100
ShowStats(lblCurrentIndex, lblPageCount, dg);
101
}
102
103
104
105
public static void PagerButtonClickGrive(object sender, EventArgs e, GridView dg, Label lblCurrentIndex, Label lblPageCount)
106
{
107
string arg = ((LinkButton)sender).CommandArgument.ToString();
108
switch (arg)
109
{
110
case "next":
111
if (dg.PageIndex < (dg.PageCount - 1))
112
{
113
dg.PageIndex += 1;
114
}
115
break;
116
case "prev":
117
if (dg.PageIndex > 0)
118
{
119
dg.PageIndex -= 1;
120
}
121
break;
122
case "last":
123
dg.PageIndex = (dg.PageCount - 1);
124
break;
125
default:
126
dg.PageIndex = System.Convert.ToInt32(arg);
127
break;
128
}
129
ShowStatsGrive(lblCurrentIndex, lblPageCount, dg);
130
}
131
}
132
然后在页面的cs文件里
protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)

{
Pages.gridViewProperty(GridView1);
getData(); //此行是我的gridview方法 根据你实际的写
Pages.ShowStatsGrive(lblCurrentIndex, lblPageCount, this.GridView1);
}
}
protected void PagerButtonClick(object sender, EventArgs e)

{
Pages.PagerButtonClickGrive(sender, e, this.GridView1, lblCurrentIndex, lblPageCount);

getData();
}
在源里gridview代码的下面
</tr>
<tr>
<td align="right" bgcolor="#F3F8FC" style="height: 41px" ><table width="100%" border="0">
<tr>
<td align="left" style="width: 142px"></td>
<td align="right"> <asp:LinkButton ID="btnFirst" runat="server" CommandArgument="0" Font-Name="verdana"
Font-Size="8pt" ForeColor="Black" OnClick="PagerButtonClick" Text="首页 "></asp:LinkButton>||
<asp:LinkButton ID="btnPrev" runat="server" CommandArgument="prev" Font-Name="verdana"
Font-Size="8pt" ForeColor="Black" OnClick="PagerButtonClick" Text=" 前一页 "></asp:LinkButton>|
<asp:LinkButton ID="btnNext" runat="server" CommandArgument="next" Font-Name="verdana"
Font-Size="8pt" ForeColor="Black" OnClick="PagerButtonClick" Text=" 下一页 "></asp:LinkButton>||
<asp:LinkButton ID="btnLast" runat="server" CommandArgument="last" Font-Name="verdana"
Font-Size="8pt" ForeColor="Black" OnClick="PagerButtonClick" Text=" 末页 "></asp:LinkButton>|
<asp:Label ID="lblCurrentIndex" runat="server" ForeColor="Black"></asp:Label>/<asp:Label ID="lblPageCount"
runat="server" ForeColor="Black"></asp:Label></td>
</tr>
</table> </td>
</tr>
posted on 2008-07-02 10:43
徐.百川 阅读(210)
评论(1) 编辑 收藏 网摘 所属分类:
.NET基础