All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
每个开发人员现在应该下载的十种必备工具,3_[Asp.Net教程]
Active,Directory如何用C#进行增加、删除、修改、查询用户与组织单位_[Asp.Net教程]
正确理解PHP程序编译时的错误信息_php资料_编程技术
如何在.NET框架下创建Access数据库和表?_[Asp.Net教程]
WM5.0模拟器用AS来使用PC机的网络_.net资料_编程技术
ASP.NET中的File类和Directory类的相关知识_.net资料_编程技术
在asp.net2.0页面部分缓存中实现缓存后替换文字内
ASP.NET技巧:用MasterPage,代替,PageBase_[Asp.Net教程]
page事件顺序(.net2.0)_[Asp.Net教程]
[delphi语法1]Object Pascal语言编写环境
Reading number is top 10 pictures
Beautiful Japanese beauty(漂亮的日本美女)2
The goddess of the single reason2
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first1
The real super beauty15
去瑜伽会所面试的经过
Female star bikini
Nikon microscopic photography of the first three
The real super beauty13
Fierce! China's special forces training the devil2
Sell the barbecue as says father du breul4
Download software ranking
Tram sex maniac 2 (H) rar bag18
The Bermuda triangle1
The Bermuda triangle2
Tram sex maniac 2 (H) rar bag2
少妇苏霞全本
Tram sex maniac 2 (H) rar bag4
Unix video tutorial14
Boxer's Top ten classic battle7
SQL2000 For 4IN1
c#程序设计案例教程
delv published in(发表于) 2014/1/23 3:11:25 Edit(编辑)
ASP.NET实例:手把手教你如何扩展GridView之个性分页_[Asp.Net教程]

ASP.NET实例:手把手教你如何扩展GridView之个性分页_[Asp.Net教程]

ASP.NET实例:手把手教你如何扩展GridView之个性分页_[Asp.Net教程]

整天面对GridView的分页,早就厌烦了,下面就谈下如何给GridView扩展出个性的分页来,首先看看运行效果图:




下面谈下重要的实现的思路的实现代码:
实现思路和上文的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();
}

这样就轻而易举的实现了一个个性的分页,欢迎各位大虾拍砖。


来源:txdlf的cnblogs








添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.