论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

手把手教你如何扩展GridView之个性分页

文章类别:Asp.net | 发表日期:2008-10-5 22:05:54

    实现思路和上文的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();
        }这样就轻而易举的实现了一个个性的分页,欢迎各位大虾拍砖。

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058