GridView自带几种分页,如果不能满足要求的话,可以看看下面的代码
上面代码中<pagertemplate>与</pagertemplate>之间为自定义分页,下面看一下触发的事件代码
1
protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
2
{
3
GridView theGrid = sender as GridView; // refer to the GridView
4
int newPageIndex = 0;
5
6
if (-2 == e.NewPageIndex)
7
{ // when click the "GO" Button
8
TextBox txtNewPageIndex = null;
9
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
10
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
11
//updated at 2006年6月21日3:15:33
12
13
if (null != pagerRow)
14
{
15
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
16
}
17
18
if (null != txtNewPageIndex)
19
{
20
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
21
}
22
}
23
else
24
{ // when click the first, last, previous and next Button
25
newPageIndex = e.NewPageIndex;
26
}
27
28
// check to prevent form the NewPageIndex out of the range
29
newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;
30
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount-1 : newPageIndex;
31
32
// specify the NewPageIndex
33
theGrid.PageIndex = newPageIndex;
34
BindNewsList();
35
// rebind the control
36
// in this case of retrieving the data using the xxxDataSoucr control,
37
// just do nothing, because the asp.net engine binds the data automatically
38
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
注意代码中最后的内容,一定要从新绑定 GridView这样分页才能够生效