在 Gridview 分页上添加“上一页,下一页,共X页,跳转到第X页”等信息

原理比较简单,
就是利用GridView 在RowCreated事件上做个手脚,

当if (e.Row.RowType == DataControlRowType.Pager) 时

加入几个Button 和Label 之后再给他们写个事件就ok了

当然我觉得最有意思的就是最后的pager里面的对象嵌套。。。


拉出来一句

e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(0, (Button_IndexFirst));

其中各个对象的含义,这里只做个参考给大家

Pannel对象->Table对象->TableRow对象->TableCell对象    恩 貌似是这样的。


雕虫小技,以博众高手一笑


以下是代码
//注意:GridView 必须开启分页功能,并启用RowCreated 事件

View Code
 1 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
2 {
3 #region 翻页绑定
4 if (e.Row.RowType == DataControlRowType.Pager)
5 {
6 Label label_Index = new Label();
7 LinkButton Button_IndexFirst = new LinkButton();
8 LinkButton Button_IndexLast = new LinkButton();
9 LinkButton Button_IndexNext = new LinkButton();
10 LinkButton Button_IndexPrevious = new LinkButton();
11
12 Button_IndexFirst.Text = "第一页 ";
13 Button_IndexFirst.CommandName = "first";
14 Button_IndexFirst.ForeColor = Color.White;
15 Button_IndexFirst.Click +=new EventHandler(PageButtonClick);
16
17 Button_IndexNext.Text = " 下一页 ";
18 Button_IndexNext.CommandName = "next";
19 Button_IndexNext.ForeColor = Color.White;
20
21 Button_IndexNext.Click += new EventHandler(PageButtonClick);
22
23 Button_IndexPrevious.Text = "前一页 ";
24 Button_IndexPrevious.CommandName = "previous";
25 Button_IndexPrevious.ForeColor = Color.White;
26 Button_IndexPrevious.Click += new EventHandler(PageButtonClick);
27
28 Button_IndexLast.Text = "最末页 ";
29 Button_IndexLast.CommandName = "last";
30 Button_IndexLast.ForeColor = Color.White;
31 Button_IndexLast.Click += new EventHandler(PageButtonClick);
32
33 label_Index.Text ="当前为第" + (GridView1.PageIndex + 1) + "页,共有"+((GridView)sender).PageCount+"";
34 e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(0, (Button_IndexFirst));
35 e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(1, (Button_IndexPrevious));
36
37 int controlTmp = e.Row.Controls[0].Controls[0].Controls[0].Controls.Count-1;
38 e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexNext);
39 e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexLast);
40
41 e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(label_Index);
42
43 //e.Row.Controls[0].Controls.Add(label_Index);
44 }
45 #endregion
46 }
47
48
49 protected void PageButtonClick(object sender, EventArgs e)
50 {
51 LinkButton clickedButton = ((LinkButton)sender);
52 if (clickedButton.CommandName == "first")
53 {
54 GridView1.PageIndex = 0;
55 }
56 else if (clickedButton.CommandName == "next")
57 {
58 if (GridView1.PageIndex < GridView1.PageCount - 1)
59 {
60 GridView1.PageIndex += 1;
61 }
62 }
63 else if (clickedButton.CommandName == "previous")
64 {
65 if (GridView1.PageIndex >= 1)
66 {
67 GridView1.PageIndex -= 1;
68 }
69 }
70 else if (clickedButton.CommandName == "last")
71 {
72 GridView1.PageIndex = GridView1.PageCount - 1;
73 }
74 }

但是上面代码实现的分页功能,不太方便设置样式,而且需要点击2次才能翻页。

其实可以有更简单的方法——使用的是GridView的  <PagerTemplate>元素。

View Code
 1 <PagerTemplate>
2 <br />
3 <asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
4 <asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>
5 <asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev" ></asp:LinkButton>
6 <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" ></asp:LinkButton>
7 <asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" ></asp:LinkButton>
8 到第<asp:TextBox runat="server" ID="inPageNum"></asp:TextBox>页 <asp:Button ID="Button1" CommandName="go" runat="server" />
9 <br />
10 </PagerTemplate>

 <asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1)  + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>

这句代码是显示数据供有几页,当前在第几页。我们通过 ((GridView)Container.NamingContainer).PageIndex来获取当前页,通过 ((GridView)Container.NamingContainer).PageCount来获取总页数。

<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页"  Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>

这一句代码实现跳转到列表的第一页,后台代码通过响应 GridView.RowCommand 事件,根据CommandName="Page"和CommandArgument="First"来定位到分页列表的第一页。GridView中的任何 一个按钮被点击都会触发RowCommand 事件,我们可以通过该事件来自定义处理程序。更多的时候建议使用GridView内置的属性。下表是MSDN上对GridView内置属性的一个简单说 明。

CommandName值

说明

“Cancel”

取消编辑操作并将 GridView 控件返回为只读模式。引发 RowCancelingEdit 事件。

“Delete”

删除当前记录。引发 RowDeletingRowDeleted 事件。

“Edit”

将当前记录置于编辑模式。引发 RowEditing 事件。

“Page”

执行分页操作。将按钮的 CommandArgument 属性设置为“First”、“Last”、“Next”、“Prev”或页码,以指定要执行的分页操作类型。引发 PageIndexChangingPageIndexChanged 事件。

“Select”

选择当前记录。引发 SelectedIndexChangingSelectedIndexChanged 事件。

“Sort”

GridView 控件进行排序。引发 SortingSorted 事件。

“Update”

更新数据源中的当前记录。引发 RowUpdating 和 RowUpdated

在这个自定义分页中,上一页,下一页,尾页和首页都使用了内置属性。

 到第<asp:TextBox runat="server" ID="inPageNum"></asp:TextBox>页 <asp:Button ID="Button1" CommandName="go" runat="server" />

这段代码是实现用户自己输入页码,然后点击Button跳转的的前台代码。为了使用RowCommand 事件,我们自定义了CommandName="go",当然你也可以在这里添加 CommandArgument以传递更多的信息。

前台代码就这些,下面我们介绍后台代码。

View Code
 1 private void BindGridView()
2 {
3 using (BlogDataContext bdc = new BlogDataContext())
4 {
5 var artList = bdc.Blog_GetAllCommentationArticles();
6 Blog_GetAllCommentationArticlesResult g = new Blog_GetAllCommentationArticlesResult();
7
8 GridView1.DataSource = artList;
9 GridView1.DataBind();
10 }
11 }
12
13 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
14 {
15 try
16 {
17 GridView1.PageIndex = e.NewPageIndex;
18 BindGridView();
19
20 TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
21 tb.Text = (GridView1.PageIndex + 1).ToString();
22 }
23 catch
24 {
25 }
26 }
27
28 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
29 {
30 if (e.CommandName == "go")
31 {
32 try
33 {
34 TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
35 int num = Int32.Parse(tb.Text);
36 GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
37 GridView1_PageIndexChanging(null, ea);
38 }
39 catch
40 {
41 }
42 }
43 }

这里主要有三个方法, BindGridView()方法,从数据库提取数据绑定到GridView控 件。 GridView1_PageIndexChanging方法,在用户单击上一页,下一页,首页,尾页的时候,通过   GridView1.PageIndex = e.NewPageIndex语句来设置GridView控件应该显示的分页数据,然后通过  TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum"); tb.Text = (GridView1.PageIndex + 1).ToString();语句在Textbox中显示当前页码。

 GridView1_RowCommand方法,在这里是响应用户自己输入页码点击Button按钮的事件。首先获取用户输入的页码数,然后调用 GridView1_PageIndexChanging方法,使GridView更新数据。







posted @ 2011-10-19 12:11  cateatmycode  阅读(1972)  评论(0编辑  收藏  举报