GridView如何实现当前页码和总记录数

2025-02-25 14:01:22
推荐回答(2个)
回答1:

参照如下代码改下html样式就好 ,不明白的留言我






' />页
共/' />页




' />





protected void grdvSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//this.grdvSearchResult.PageIndex = e.NewPageIndex;
////this.DataBind();

DataTable aaa = new DataTable();
aaa = (DataTable)ViewState["TableForAllSelect"];

grdvSearchResult.DataSource = aaa;
grdvSearchResult.DataBind();
grdvSearchResult.Visible = true;

GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;

if (-2 == e.NewPageIndex)
{ // when click the "GO" Button
TextBox txtNewPageIndex = null;
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33

if (null != pagerRow)
{
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
}

if (null != txtNewPageIndex)
{
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
}
else
{ // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}

// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;

}

回答2:

RowDataBound() 是在加载GridView是,每加载一行数据,就只行一次这函数,当加载完最后一行时,还会执行一遍,一判断是否全部加载完成,这时候就抛出System.StackOverflowException(栈溢出)的异常。 为什么你要放在这个函数中呢?要显示只需在数据全部绑定完后,计算行数显示就行了。