GridView控件实现分页技术
第一步:设置GridView控件的属性,跟分页相关的属性设置如下:
AllowPaging="true":允许分页,
PageSize="大小":每一页显示的信息条数,
OnPageIndexChanging="GridView1_PageIndexChanging":页数改变时触发的事件。
第二步:设置GridView控件的PagerTemplate页模板属性。PageTemplate是用来获取或设置
GridView 控件中页导航行的自定义内容。代码实现如下:
1 <PagerTemplate>
2
3
<table width="100%">
4
5 <tr>
6
7 <td
style="text-align: center">
8
9
10
11 第<asp:Label
ID="lblPageIndex" runat="server"
12
13
Text="<%#((GridView)Container.Parent.Parent)。PageIndex + 1 %>"
></asp:Label>页
14
15
16
17 共<asp:Label
ID="lblPageCount" runat="server" Text="<%#
((GridView)Container.Parent.Parent)。PageCount
%>"></asp:Label>页
18
19
20
21 <asp:LinkButton
ID="btnFirst" runat="server" CausesValidation="False"
CommandArgument="First"
22 CommandName="Page"
Text="首页"></asp:LinkButton>
23
24
25
26
<asp:LinkButton ID="btnPrev" runat="server"
27 CausesValidation="False"
CommandArgument="Prev"
28 CommandName="Page"
Text="上一页"></asp:LinkButton>
29
30
31
32
<asp:LinkButton ID="btnNext" runat="server"
33 CausesValidation="False"
CommandArgument="Next"
34 CommandName="Page"
Text="下一页"></asp:LinkButton>
35
36
37
38
<asp:LinkButton ID="btnLast" runat="server"
39 CausesValidation="False"
CommandArgument="Last"
40 CommandName="Page"
Text="尾页"></asp:LinkButton>
41
42
43
44
<asp:TextBox ID="txtNewPageIndex" runat="server"
45 Text="<%#
((GridView)Container.Parent.Parent)。PageIndex + 1%>"
Width="20px"></asp:TextBox>
46
47
48
49
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False"
CommandArgument="-1" CommandName="Page"
Text="GO"></asp:LinkButton>
50
51
52
53
</td>
54
55 </tr>
56
57
</table>
58
59 </PagerTemplate>
View
Code
((GridView)Container.Parent.Parent)。PageIndex +
1获取当前控件显示的页面数;((GridView)Container.Parent.Parent)。PageCount获取当前控件总的页面数;CausesValidation设置按钮提交时不执行验证;CommandArgument设置与关联的
CommandName 属性一起传递到 Command 事件处理程序的可选参数;CommandName设置命令名,该命令名与传递给 Command 事件的
Button 控件相关联 www.yztrans.com
第三步:后台GridView1_PageIndexChanging事件的实现,代码如下:
protected void
GridView1_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
//通过类型转换得到当前的gridview控件
GridView gvw =
(GridView)sender;
if (e.NewPageIndex < 0)
{
TextBox PageNum =
(TextBox)GridView1.BottomPagerRow.FindControl("txtNewPageIndex");
int Pa =
int.Parse(PageNum.Text);
if (Pa <=
0)//如果前往分页的index小于或等于0则转向0
{
gvw.PageIndex =
0;
}
else
{
gvw.PageIndex = Pa -
1;
}
}
else
{
gvw.PageIndex =
e.NewPageIndex;
}
bind();//自定义的GridView控件绑定数据函数
} www.tygj123.com
分页技术之GridView控件,布布扣,bubuko.com
原文:http://www.cnblogs.com/tianchaoy/p/3659291.html