ASP.NET中 DataGrid简单自定义分页
先在pageload中添加事件
基本上就这些,搞定了。
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//分页
First.Click += new System.EventHandler(this.NavigationButtonClick);
Previous.Click += new System.EventHandler(this.NavigationButtonClick);
Next.Click += new System.EventHandler(this.NavigationButtonClick);
Last.Click += new System.EventHandler(this.NavigationButtonClick);
}
窗体里添加4个linkbutton按钮,设置其CommandName分别为:FIRST、PREVIOUS、NEXT、LAST NavigationButtonClick事件代码如下{
// 在此处放置用户代码以初始化页面
//分页
First.Click += new System.EventHandler(this.NavigationButtonClick);
Previous.Click += new System.EventHandler(this.NavigationButtonClick);
Next.Click += new System.EventHandler(this.NavigationButtonClick);
Last.Click += new System.EventHandler(this.NavigationButtonClick);
}
private void NavigationButtonClick(object sender, System.EventArgs e)
{
string direction = ((LinkButton)sender).CommandName;
switch (direction.ToUpper())
{
case "FIRST" :
DataGrid1.CurrentPageIndex = 0;
break;
case "PREVIOUS" :
DataGrid1.CurrentPageIndex =
Math.Max(DataGrid1.CurrentPageIndex -1, 0);
break;
case "NEXT" :
DataGrid1.CurrentPageIndex =
Math.Min(DataGrid1.CurrentPageIndex + 1,
DataGrid1.PageCount - 1);
break;
case "LAST" :
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1;
break;
default :
break;
}
DataGrid1.DataBind();
}
{
string direction = ((LinkButton)sender).CommandName;
switch (direction.ToUpper())
{
case "FIRST" :
DataGrid1.CurrentPageIndex = 0;
break;
case "PREVIOUS" :
DataGrid1.CurrentPageIndex =
Math.Max(DataGrid1.CurrentPageIndex -1, 0);
break;
case "NEXT" :
DataGrid1.CurrentPageIndex =
Math.Min(DataGrid1.CurrentPageIndex + 1,
DataGrid1.PageCount - 1);
break;
case "LAST" :
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1;
break;
default :
break;
}
DataGrid1.DataBind();
}
基本上就这些,搞定了。