如何扩展GridView之个性分页

原创 2007年09月29日 12:30:00
实现思路和上文的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 = "首页";
            btnFirst.Command += new CommandEventHandler(NavigateToPage);
            btnFirst.CommandName = "Pager";
            btnFirst.CommandArgument = "First";

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

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

            btnLast = new LinkButton();
            btnLast.Text = "尾页";
            btnLast.Command += new CommandEventHandler(NavigateToPage);
            btnLast.CommandName = "Pager";
            btnLast.CommandArgument = "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 (e.CommandArgument.ToString())
            {
                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();
        }

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

怎么使用PHP和MySQL创建个性的网站分页

分页起着重要的作用,在任何Web应用程序,具有足够大的数据被分成页。它不仅外观整洁,但也提高了一个网页的加载时间。所以,分页是很重要的一个改进的用户界面,并节省服务器资源。在本教程中,我会告诉你一个简...

gridview分页效果

  • 2015-11-27 14:25
  • 1.24MB
  • 下载

gridview分页效果

  • 2016-08-28 11:16
  • 1.24MB
  • 下载

Android自定义控件(一) 下拉刷新,上拉分页加载更多(支持ListView, GridView, ScrollView)

项目中用到了下拉刷新和上啦加载

android gridview分页效果

  • 2014-12-22 20:18
  • 1.24MB
  • 下载

GridView分页

  • 2014-12-31 11:51
  • 811KB
  • 下载

AspNetPager与gridview结合实现分页

使用AspNetPager和GridView实现分页的简单例子  1.首先 简单介绍一下AspNetPager这个分页组件  AspNetPager控件和数据是独立的,将分页导航功能与数据显示功能...

asp.net 中gridview控件分页的实现

要在GridView中加入 //实现分页 AllowPaging="true" //一页数据10行 PageSize="10" // 分页时触发的事件 OnPageIndexChangin...
  • uniqer
  • uniqer
  • 2012-07-17 18:04
  • 5268
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)