国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

手把手教你如何擴(kuò)展GridView之個(gè)性分頁

2019-11-18 16:31:20
字體:
供稿:網(wǎng)友

    實(shí)現(xiàn)思路和上文的ExcelWord導(dǎo)出是一樣的,就是在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);
        }

然后是關(guān)鍵部分的代碼,就是將這些控件如何添加到GridView中,通過在創(chuàng)建子控件的方式,如下:
在創(chuàng)建子控件的方法中添加分頁控件
   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();
        }這樣就輕而易舉的實(shí)現(xiàn)了一個(gè)個(gè)性的分頁,歡迎各位大蝦拍磚。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通榆县| 青海省| 东阳市| 阳泉市| 宣城市| 桂东县| 宁都县| 东安县| 汽车| 诸城市| 留坝县| 睢宁县| 始兴县| 河东区| 托克托县| 辽宁省| 普陀区| 环江| 丁青县| 仲巴县| 阿坝县| 汕头市| 平度市| 衡水市| 仁寿县| 西城区| 台山市| 五华县| 渝北区| 五家渠市| 凤凰县| 镇安县| 沙洋县| 炎陵县| 靖西县| 石渠县| 盘锦市| 金门县| 富源县| 阜平县| 忻州市|