给GridView分页
private void Bind()
{
GV1.DataSource = newsbll.SelectNews();
GV1.DataBind();
//分页
int pageAll = Convert.ToInt32(GV1.PageCount.ToString());
this.LblCurrentIndex.Text = "第 " + (GV1.PageIndex + 1).ToString() + " 页";
this.LblPageCount.Text = "共 " + pageAll + " 页";
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName = "1";
btnPrev.CommandName = (GV1.PageIndex == 0 ? "1" : GV1.PageIndex.ToString());
btnNext.CommandName = (GV1.PageCount == 1 ? pageAll.ToString() : (GV1.PageIndex + 2).ToString());
btnLast.CommandName = pageAll.ToString();
ddlpage.Items.Clear();
if (pageAll > 1)
{
for (int i = 1; i <= pageAll; i++)
{
ddlpage.Items.Add(new ListItem("" + i, "" + i));
}
ddlpage.Items.FindByValue((GV1.PageIndex + 1).ToString()).Selected = true;
ddlpage.Visible = true;
noinfodiv.Visible = false;
pagediv.Visible = true;
}
else if (pageAll == 1)
{
pagediv.Visible = false;
noinfodiv.Visible = false;
}
else
{
pagediv.Visible = false;
noinfodiv.Visible = true;
}
}
/// <summary>
/// 分页 上一页 下一页 首页 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PagerButtonClick(object sender, EventArgs e)
{
GV1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
Bind();
}
/// <summary>
/// 转页面dorpListDown
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlpage_SelectedIndexChanged(object sender, EventArgs e)
{
GV1.PageIndex = Convert.ToInt32(ddlpage.SelectedValue) - 1;
ddlpage.SelectedIndex = GV1.PageIndex;
Bind();
}