你如何扩展GridView之个性分页

时间:2010-02-04 13:03:50  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:bsp; Label lblPageCount;    Label lblRowsCount;    LinkButton btnFirst;    LinkButton btnPrev;    LinkButton btnNext;    LinkButton btnLast;在GridView的OnInit方法中,初始化这些控件   在控件的Oninit方法初始化分页控件    protected override void OnIni...
  正文:实现思路和上文的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);
   Table
  
   Cell 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(); 《你如何扩展GridView之个性分页》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/aspnet/12776.html


关键字:

关于《你如何扩展GridView之个性分页》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td