整天面对GridView的分页,早就厌烦了,下面就谈下如何给GridView扩展出个性的分页来,首先看看运行效果图:
下面谈下重要的实现的思路的实现代码:
实现思路和上文的Excel和Word导出是一样的,就是在GridView中添加行,首先声明以下控件,用于显示页次:第几页,共多少页,多少记录,首页,上一页,下一页,尾页
用于分页的控件
在GridView的OnInit方法中,初始化这些控件在控件的Oninit方法初始化分页控件
然后是关键部分的代码,就是将这些控件如何添加到GridView中,通过在创建子控件的方式,如下:
在创建子控件的方法中添加分页控件
下面就是处理分页的事件,类似于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();
}
这样就轻而易举的实现了一个个性的分页,欢迎各位大虾拍砖。{
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之自带Excel和Word导出
手把手教你如何扩展GridView之自带分页
手把手教你如何扩展GridView之自带CheckBox
手把手教你如何扩展GridView之自动排序篇