这是一个GridView控件分页问题,如果是使用的VS中自身提供的数据源控件的话,没什么问题,直接可以选择”分页”功能,可是如果自己用代码设置数据源时就易出现这样的错误——-GridView“GridView1”激发了未处理的事件“PageIndexChanging”.
修改方法:一要在girdview 后面加
OnPageIndexChanging="GridView1_PageIndexChanging
二要cs页面中添加
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataBind();
}
OK问题解决!
下面的是一个比较齐全的分页代码示例:
protected void Page_Load(object sender, EventArgs e)
{
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
GridView1.DataBind();
ShowPageChangedStatus();
}
private void ShowPageChangedStatus()
{
lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
lblPageCount.Text = "总共 " + GridView1.PageCount.ToString() + " 页";
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (GridView1.PageIndex < (GridView1.PageCount - 1))
{
GridView1.PageIndex += 1;
}
break;
case "prev":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
case "last":
GridView1.PageIndex = (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex = System.Convert.ToInt32(arg);
break;
}
GridView1.DataBind();
ShowPageChangedStatus();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex;
startIndex = GridView1.PageIndex * GridView1.PageSize;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
ShowPageChangedStatus();
}
--------------------------------------------------------------------------------