标题:手把手教你如何扩展GridView之个性分页
只看楼主
重庆萤火虫
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2008-3-6
 问题点数:0 回复次数:2 
手把手教你如何扩展GridView之个性分页
手把手教你如何扩展GridView之个性分页
实现思路和上文的Excel和Word导出是一样的,就是在GridView中添加行,首先声明以下控件,用于显示页次:第几页,共多少页,多少

记录,首页,上一页,下一页,尾页
用于分页的控件
  Label lblCurrentPage;
        Label lblPageCount;
        Label lblRowsCount;
        LinkButton btnFirst;
        LinkButton btnPrev;
        LinkButton btnNext;
        LinkButton btnLast;在GridView的OnInit方法中,初始化这些控件
在控件的Oninit方法初始化分页控件
 protected override void OnInit(EventArgs e)
        {
            this.EnableViewState = true;         

            lblCurrentPage = new Label();
            lblCurrentPage.ForeColor = ColorTranslator.FromHtml("#e78a29");
            lblCurrentPage.Text = "1";

            lblPageCount = new Label();
            lblPageCount.Text = "1";


            lblRowsCount = new Label();
            lblRowsCount.ForeColor = ColorTranslator.FromHtml("#e78a29");

            btnFirst = new LinkButton();
            btnFirst.Text = "首页";
             += new CommandEventHandler(NavigateToPage);
             = "Pager";
             = "First";

            btnPrev = new LinkButton();
            btnPrev.Text = "上一页";
             += new CommandEventHandler(NavigateToPage);
             = "Pager";
             = "Prev";

            btnNext = new LinkButton();
            btnNext.Text = "下一页";
             += new CommandEventHandler(NavigateToPage);
             = "Pager";
             = "Next";

            btnLast = new LinkButton();
            btnLast.Text = "尾页";
             += new CommandEventHandler(NavigateToPage);
             = "Pager";
             = "Last";

            base.OnInit(e);
        }

然后是关键部分的代码,就是将这些控件如何添加到GridView中,通过在创建子控件的方式,如下:
在创建子控件的方法中添加分页控件
   protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
        {
            int res = base.CreateChildControls(dataSource, dataBinding);
            if (ShowToolBar)
            {
                try
                {
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Pager, DataControlRowState.Normal);
                    TableCell c = new TableCell();
                    c.Width = Unit.Percentage(100);
                    c.ColumnSpan = this.Columns.Count;                  
                    row.Cells.Add(c);
                    TableCell cell1 = new TableCell();
                    Table table = new Table();
                    TableRow r = new TableRow();
                    table.Rows.Add(r);
                    table.Width = Unit.Percentage(100);
                    c.Controls.Add(table);
                    r.Cells.Add(cell1);
                    Literal l1 = new Literal();
                    l1.Text = "页次:";
                    cell1.Controls.Add(l1);
                    cell1.Wrap = false;
                    cell1.Controls.Add(lblCurrentPage);
                    l1 = new Literal();
                    l1.Text = "页/";
                    cell1.Controls.Add(l1);
                    cell1.Controls.Add(lblPageCount);
                    l1 = new Literal();
                    l1.Text = "页,共";
                    cell1.Controls.Add(l1);
                    cell1.Controls.Add(lblRowsCount);
                    l1 = new Literal();
                    l1.Text = "条记录";
                    cell1.HorizontalAlign = HorizontalAlign.Left;
                    cell1.Controls.Add(l1);
                    TableCell cell2 = new TableCell();
                    cell2.HorizontalAlign = HorizontalAlign.Right;
                    cell2.Wrap = false;         


                    l1 = new Literal();
                    l1.Text = " [";
                    cell2.Controls.Add(l1);
                    cell2.Controls.Add(btnFirst);
                    l1 = new Literal();
                    l1.Text = "] ";
                    cell2.Controls.Add(l1);

                    l1 = new Literal();
                    l1.Text = " [";
                    cell2.Controls.Add(l1);
                    cell2.Controls.Add(btnPrev);
                    l1 = new Literal();
                    l1.Text = "] ";
                    cell2.Controls.Add(l1);

                    l1 = new Literal();
                    l1.Text = " [";
                    cell2.Controls.Add(l1);
                    cell2.Controls.Add(btnNext);
                    l1 = new Literal();
                    l1.Text = "] ";
                    cell2.Controls.Add(l1);

                    l1 = new Literal();
                    l1.Text = " [";
                    cell2.Controls.Add(l1);
                    cell2.Controls.Add(btnLast);
                    l1 = new Literal();
                    l1.Text = "] ";
                    cell2.Controls.Add(l1);
                    r.Cells.Add(cell2);
                    this.Controls[0].Controls.AddAt(0, row);
                }
                catch
                {
                }
            }
            return res;
        }下面就是处理分页的事件,类似于RowCommand
 public void NavigateToPage(object sender, CommandEventArgs e)
        {
            btnFirst.Enabled = true;
            btnPrev.Enabled = true;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
            switch (())
            {
                case "Prev":
                    if (this.PageIndex > 0)
                    {
                        this.PageIndex -= 1;

                    }
                    break;
                case "Next":
                    if (this.PageIndex < (this.PageCount - 1))
                    {
                        this.PageIndex += 1;

                    }
                    break;
                case "First":
                    this.PageIndex = 0;
                    break;
                case "Last":
                    this.PageIndex = this.PageCount - 1;
                    break;
            }
            if (this.PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (this.PageCount == 1)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (this.PageIndex == this.PageCount - 1)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
            OnBind();
        }这样就轻而易举的实现了一个个性的分页,欢迎各位大虾拍砖。
搜索更多相关主题的帖子: GridView 手把手 
2008-03-19 16:21
beniao
Rank: 2
等 级:新手上路
威 望:4
帖 子:367
专家分:2
注 册:2004-12-17
得分:0 
拍个头拍
  你这个方法是最原始的
     .....
 现在的主流都不用这个方法.

博客:http://
2008-03-19 20:45
寂寞的刺猬
Rank: 1
等 级:新手上路
帖 子:111
专家分:0
注 册:2008-3-23
得分:0 
代码写的有点乱,不过还是看明白了。或许benniao有更通用的分页方法,只可惜没共享出来。

我的博客  http://wlb.wlb.blog.
2008-03-23 17:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-204230-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.247262 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved