实现思路和上文的Excel和Word导出是一样的,就是在GridView中添加行,首先声明以下控件,用于显示页次:第几页,共多少页,多少记录,首页,上一页,下一页,尾页 lblCurrentPage = new Label(); lblPageCount = new Label(); btnFirst = new LinkButton(); btnPrev = new LinkButton(); btnNext = new LinkButton(); btnLast = new LinkButton(); base.OnInit(e); 然后是关键部分的代码,就是将这些控件如何添加到GridView中,通过在创建子控件的方式,如下: l1 = new Literal(); l1 = new Literal(); l1 = new Literal(); } }
用于分页的控件
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.ForeColor = ColorTranslator.FromHtml(“#e78a29”);
lblCurrentPage.Text = “1”;
lblPageCount.Text = “1”;
lblRowsCount = new Label();
lblRowsCount.ForeColor = ColorTranslator.FromHtml(“#e78a29”);
btnFirst.Text = “首页”;
btnFirst.Command += new CommandEventHandler(NavigateToPage);
btnFirst.CommandName = “Pager”;
btnFirst.CommandArgument = “First”;
btnPrev.Text = “上一页”;
btnPrev.Command += new CommandEventHandler(NavigateToPage);
btnPrev.CommandName = “Pager”;
btnPrev.CommandArgument = “Prev”;
btnNext.Text = “下一页”;
btnNext.Command += new CommandEventHandler(NavigateToPage);
btnNext.CommandName = “Pager”;
btnNext.CommandArgument = “Next”;
btnLast.Text = “尾页”;
btnLast.Command += new CommandEventHandler(NavigateToPage);
btnLast.CommandName = “Pager”;
btnLast.CommandArgument = “Last”;
}
在创建子控件的方法中添加分页控件
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.Text = ” [“;
cell2.Controls.Add(l1);
cell2.Controls.Add(btnPrev);
l1 = new Literal();
l1.Text = “] “;
cell2.Controls.Add(l1);
l1.Text = ” [“;
cell2.Controls.Add(l1);
cell2.Controls.Add(btnNext);
l1 = new Literal();
l1.Text = “] “;
cell2.Controls.Add(l1);
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();
}这样就轻而易举的实现了一个个性的分页,欢迎各位大虾拍砖。
手把手教你如何扩展gridview之个性分页_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 手把手教你如何扩展gridview之个性分页_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧