前些天我写了关于 <<在存储过程中实现分页>>和<<GridView控件事件详解 >>
,后来又有一些人问我怎样在GridView中应用这个东东!其实很简单,主要是怎么保存当前页面的页码PageIndex问题,不过把这个解决了什么都好办了.因为在分页过程中:PageSize是一定的,我们可以用一个属性来表示.保存PageIndex好多中方法,而且数据不是很庞大,基本不会好太多的资源.还是一句老话,话再多都没有例子直观.
在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!
在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!
1
<
asp:GridView
ID
="NewsGrid"
runat
="server"
AutoGenerateColumns
="False"
AllowPaging
="false"
Width
="100%"
>
2
<
Columns
>
3
<
asp:BoundField
DataField
="NewsId"
HeaderText
="新闻ID"
/>
4
<
asp:HyperLinkField
DataNavigateUrlFields
="NewsId"
DataNavigateUrlFormatString
="~/Details.aspx?ID={0}"
5
DataTextField
="Title"
HeaderText
="新闻标题"
ItemStyle-Width
="70%"
/>
6
<
asp:BoundField
DataField
="PostTime"
HeaderText
="发布时间"
/>
7
<
asp:CommandField
HeaderText
="新闻管理"
ShowCancelButton
="False"
ShowDeleteButton
="True"
8
ShowEditButton
="True"
/>
9
</
Columns
>
10
</
asp:GridView
>
11
<
div
style
=" height:16px; padding-top:5px; margin-right:30px; float:right"
>
12
<
asp:HiddenField
ID
="CurrentPage"
runat
="server"
Value
="0"
/>
13
<
asp:LinkButton
ID
="First"
runat
="server"
CommandArgument
="first"
OnClick
="PagerButton_Click"
>
首 页
</
asp:LinkButton
>
14
<
asp:LinkButton
ID
="Prev"
runat
="server"
CommandArgument
="prev"
OnClick
="PagerButton_Click"
>
上一页
</
asp:LinkButton
>
15
<
asp:LinkButton
ID
="Next"
runat
="server"
CommandArgument
="next"
OnClick
="PagerButton_Click"
>
下一页
</
asp:LinkButton
>
16
<
asp:LinkButton
ID
="Last"
runat
="server"
CommandArgument
="last"
OnClick
="PagerButton_Click"
>
尾 页
</
asp:LinkButton
>
17
</
div
>
CS文件中的代码:![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
3
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
4
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
5
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
6
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
7
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
8
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
9
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
10
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
11
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
12
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
13
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
14
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
15
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
16
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
17
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
1
protected
void
PagerButton_Click(
object
sender, EventArgs e)
2
{
3
int pageIndx = Convert.ToInt32(CurrentPage.Value);
4
int totals = NewsManager.GetNews(0, pageSize).TotalRecords;
5
int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1);
6
string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
7
switch (arg)
8
{
9
case "prev":
10
if (pageIndx > 0)
11
{
12
pageIndx -= 1;
13
}
14
break;
15
case "next":
16
if (pageIndx < pages - 1)
17
{
18
pageIndx += 1;
19
}
20
break;
21
case "last":
22
pageIndx = pages - 1;
23
break;
24
default:
25
pageIndx = 0;
26
break;
27
}
28
CurrentPage.Value = pageIndx.ToString();
29
NewsGrid.DataSource = NewsManager.GetNews(pageIndx , pageSize).Entities;
30
NewsGrid.DataBind();
31
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
3
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
4
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
5
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
6
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
7
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
8
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
10
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
11
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
12
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
13
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
14
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
15
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
16
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
17
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
18
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
19
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
20
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
21
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
22
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
23
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
24
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
25
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
26
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
27
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
28
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
29
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
30
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
31
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
是不是很简单啊,看了代码就都懂了,关于用按钮传参数在这里就不多说了,还有一个就是Entities属性,在<<应用实体类EntitySet实现类似的范型功能 >>这篇文章中可以找到!
本文转自网魂小兵博客园博客,原文链接:http://www.cnblogs.com/xdotnet/archive/2006/09/29/gridview_paging_myself.html,如需转载请自行联系原作者