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事件代码如下
        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 
-10);
                    
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();
        }

基本上就这些,搞定了。

posted on 2006-11-17 16:24  散步的蠕虫  阅读(166)  评论(0编辑  收藏  举报

导航