设置GridView中,底部的一个Label的Text 的代码:
GridViewRow pagerRow = GridView1.BottomPagerRow;
// 获取一个GridViewRow对象,该对象表示GridView控件中的底部页导航行
Label pageLabel = (Label)pagerRow.FindControl("Label_now");
///////////////
//非常重要
///////////////
说明一下为什么用FindControl而不用GridViewRow.Cells[0]
GridViewRow.Cells[0]只能用在Table里面,<tb></tb>的形式中
而导航按钮不在GridView的Table里,因此只能用FindControl去找
if ( pageLabel!= null)
{
// Calculate the current page number.
int currentPage = GridView1.PageIndex+1;
// Update the Label control with the current page information.
pageLabel.Text = currentPage.ToString();
}
然而没有实际意义
因为GridView最后要重新绑定数据.所以pageLabel.Text 会被重新设置
但调用GridView中控件的算法是一样的
但是貌似最后绑定数据也没有意义,因为翻页并不改变数据,而绑定的动作已经在点击按钮的时候执行过了
因此我决定还是用这种方法来设置导航按钮
protected void PageCut_Click(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
// 获取一个GridViewRow对象,该对象表示GridView控件中的底部页导航行
Label pageLabel = (Label)pagerRow.FindControl("Label_now");
string commandArg = ((LinkButton)sender).CommandArgument;
//// ((LinkButton)sender).CommandArgument用来获取点击按钮的CommandArgument;
///原来是这样获取的啊
switch (commandArg)
{
case "First":
//GridView1.PageIndex = 0;
if ( pageLabel!= null)
{
// Calculate the current page number.
int currentPage = GridView1.PageIndex+1;
// Update the Label control with the current page information.
pageLabel.Text = currentPage.ToString();
}
break;
case "Prev":
GridView1.PageIndex=(int)Math.Max(0,GridView1.PageIndex-1);
break;
case "Next":
GridView1.PageIndex = (int)Math.Min(GridView1.PageCount - 1, GridView1.PageIndex + 1);
break;
case "Last":
GridView1.PageIndex = GridView1.PageCount - 1;
break;
}
//binddata();
}
然后又发现问题:
在点击"下一页"按钮到了最后一页后,再点一次时页面跳转到了不知道哪一页,因为导航按钮都不见了!!!???(灵异事件??)
发现加上数据绑定后就不会出现上述问题,但绑定后的初始化仍然使pagerLabel.Text 被重置,烦
然后HXY用DetailsView搞定了
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
Label pageLabel = (Label)pagerRow.FindControl("Label_now");
if ((pageLabel != null))
{
// Update the Label controls with the current page values.
int page = GridView1. + 1;
// int count = CustomerDetailView.DataItemCount;
pageLabel.Text = page.ToString();
//totalNum.Text = count.ToString() ;
}
换了我只能在Lable的属性里设置数据绑定,就是这样了