您的位置:首页 > 其它

在 Gridview 分页上添加“上一页,下一页,共X页,跳转到第X页”等信息

2011-10-19 12:11 357 查看
原理比较简单,
就是利用GridView 在RowCreated事件上做个手脚,

当if (e.Row.RowType == DataControlRowType.Pager) 时

加入几个Button 和Label 之后再给他们写个事件就ok了

当然我觉得最有意思的就是最后的pager里面的对象嵌套。。。

拉出来一句

e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(0, (Button_IndexFirst));

其中各个对象的含义,这里只做个参考给大家

Pannel对象->Table对象->TableRow对象->TableCell对象 恩 貌似是这样的。

雕虫小技,以博众高手一笑

以下是代码
//注意:GridView 必须开启分页功能,并启用RowCreated 事件

View Code

private void BindGridView()
{
using (BlogDataContext bdc = new BlogDataContext())
{
var artList = bdc.Blog_GetAllCommentationArticles();
Blog_GetAllCommentationArticlesResult g = new Blog_GetAllCommentationArticlesResult();

GridView1.DataSource = artList;
GridView1.DataBind();
}
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();

TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
tb.Text = (GridView1.PageIndex + 1).ToString();
}
catch
{
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
try
{
TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
int num = Int32.Parse(tb.Text);
GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
GridView1_PageIndexChanging(null, ea);
}
catch
{
}
}
}


这里主要有三个方法, BindGridView()方法,从数据库提取数据绑定到GridView控 件。 GridView1_PageIndexChanging方法,在用户单击上一页,下一页,首页,尾页的时候,通过 GridView1.PageIndex = e.NewPageIndex语句来设置GridView控件应该显示的分页数据,然后通过 TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum"); tb.Text = (GridView1.PageIndex + 1).ToString();语句在Textbox中显示当前页码。

GridView1_RowCommand方法,在这里是响应用户自己输入页码点击Button按钮的事件。首先获取用户输入的页码数,然后调用 GridView1_PageIndexChanging方法,使GridView更新数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐