增强 GridView 控件的功能

还记得雅虎的XX(不好意思,忘记姓甚名谁了)有一篇演讲,其中一个字就是“懒”。咱写代码的,不懒点儿还真是吃不消,尤其是现在这样要命的天气~~。

相信大家都多少有抱怨GridView控件的功能吧,咱在这就不多说了,贴出代码是最重要的。

效果图

该类增强了微软的GridView的功能,增加了“首页”、“上页”、“下页”、“尾页”按钮,方便大家使用,翻页事件也已经添加好,在相关页面只要添加以下代码就可以了:

     protected   void  Page_Init( object  sender, EventArgs e) {
        Lyout.Web.Extension.GridView.RegisterEvents(listTable, 
new  Lyout.Web.Extension.GridViewDataBind(BindData));
    }

记住必须在 Page_Init 里面。其中 listTable 为GridView控件的ID,BindData是自己绑定数据的方法的名称。

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace  Lyout.Web.Extension  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// GridView 数据绑定委托
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public delegate void GridViewDataBind();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 增强 GridView 的功能
ExpandedSubBlockEnd.gif    
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif    public sealed class GridView dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="gridView">需要注册事件的GridView控件</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) dot.gif{
InBlock.gif            RegisterEvents(gridView, 
nulltrue);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="gridView">需要注册事件的GridView控件</param>
ExpandedSubBlockEnd.gif        
/// <param name="dataBind">数据绑定方法</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) dot.gif{
InBlock.gif            RegisterEvents(gridView, dataBind, 
true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="gridView">需要注册事件的GridView控件</param>
InBlock.gif        
/// <param name="dataBind">数据绑定方法</param>
ExpandedSubBlockEnd.gif        
/// <param name="pageText">翻页按钮上的文字</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) dot.gif{
InBlock.gif            RegisterEvents(gridView, dataBind, pageText, 
true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="gridView">需要注册事件的GridView控件</param>
InBlock.gif        
/// <param name="dataBind">数据绑定方法</param>
ExpandedSubBlockEnd.gif        
/// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) dot.gif{
InBlock.gif            RegisterEvents(gridView, dataBind, 
null, autoChangePage);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="gridView">需要注册事件的GridView控件</param>
InBlock.gif        
/// <param name="dataBind">数据绑定方法</param>
InBlock.gif        
/// <param name="pageText">翻页按钮上的文字</param>
ExpandedSubBlockEnd.gif        
/// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            gridView.RowCommand 
+= delegate(object sender, GridViewCommandEventArgs e) dot.gif{
InBlock.gif                RowCommand(sender, e, dataBind);
ExpandedSubBlockEnd.gif            }
;
ExpandedSubBlockStart.gifContractedSubBlock.gif            gridView.RowCreated 
+= delegate(object sender, GridViewRowEventArgs e) dot.gif{
InBlock.gif                RowCreated(sender, e, pageText);
ExpandedSubBlockEnd.gif            }
;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (autoChangePage) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                gridView.PageIndexChanging 
+= delegate(object sender, GridViewPageEventArgs e) dot.gif{
InBlock.gif                    gridView.PageIndex 
= e.NewPageIndex;
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if (dataBind != nulldot.gif{
InBlock.gif                        dataBind();
ExpandedSubBlockStart.gifContractedSubBlock.gif                    }
 else dot.gif{
InBlock.gif                        gridView.DataBind();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }
;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RowCommand(object sender, GridViewCommandEventArgs e) dot.gif{
InBlock.gif            RowCommand(sender, e, 
null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="function">数据绑定方法</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) dot.gif{
InBlock.gif            System.Web.UI.WebControls.GridView _grid 
= (System.Web.UI.WebControls.GridView)sender;
InBlock.gif            SetPageIndex(_grid, e.CommandName);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (dataBind != nulldot.gif{
InBlock.gif                dataBind();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif{
InBlock.gif                _grid.DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 增加首页、上页、下页、尾页按钮
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RowCreated(object sender, GridViewRowEventArgs e) dot.gif{
InBlock.gif            RowCreated(sender, e, 
null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 增加首页、上页、下页、尾页按钮
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(e.Row.RowType == DataControlRowType.Pager) dot.gif{
InBlock.gif                System.Web.UI.WebControls.GridView _grid 
= (System.Web.UI.WebControls.GridView)sender;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(_grid.PagerTemplate == nulldot.gif{
InBlock.gif                    _grid.PagerSettings.Mode 
= PagerButtons.Numeric;
InBlock.gif
InBlock.gif                    
int pageIndex = _grid.PageIndex;
InBlock.gif                    
int pageCount = _grid.PageCount;
InBlock.gif
InBlock.gif                    TableRow objRow 
= (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
InBlock.gif                    TableCell objCell;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
string[] button = dot.gif{"首页","上页","下页","尾页"};
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(buttonText != nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
for(int i = 0; i < buttonText.Length; i++dot.gif{
InBlock.gif                            button[i] 
= buttonText[i];
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
bool enabled = true;
InBlock.gif
InBlock.gif                    enabled 
= pageIndex > 0 ? true : false;
InBlock.gif
InBlock.gif                    
// 增加首页、上一页按钮
InBlock.gif
                    objCell = new TableCell();
InBlock.gif                    objCell.Controls.Add(LinkButton(button[
0], "Page$First""", enabled));
InBlock.gif                    objRow.Cells.AddAt(
0, objCell);
InBlock.gif
InBlock.gif                    objCell 
= new TableCell();
InBlock.gif                    objCell.Controls.Add(LinkButton(button[
1], "Page$Prev""", enabled));
InBlock.gif                    objRow.Cells.AddAt(
1, objCell);
InBlock.gif
InBlock.gif                    enabled 
= pageIndex < (pageCount - 1? true : false;
InBlock.gif
InBlock.gif                    
// 增加尾页、下一页按钮
InBlock.gif
                    objCell = new TableCell();
InBlock.gif                    objCell.Controls.Add(LinkButton(button[
2], "Page$Next""", enabled));
InBlock.gif                    objRow.Cells.Add(objCell);
InBlock.gif
InBlock.gif                    objCell 
= new TableCell();
InBlock.gif                    objCell.Controls.Add(LinkButton(button[
3], "Page$Last""", enabled));
InBlock.gif                    objRow.Cells.Add(objCell);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置GridView的页索引
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="command">被单击的按钮的命令</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
switch(command) dot.gif{
InBlock.gif                
case "Page$First":
InBlock.gif                    gridView.PageIndex 
= 0;
InBlock.gif                    
break;
InBlock.gif                
case "Page$Prev":
InBlock.gif                    
if(gridView.PageIndex > 0)
InBlock.gif                        gridView.PageIndex 
-= 1;
InBlock.gif                    
break;
InBlock.gif                
case "Page$Next":
InBlock.gif                    
if(gridView.PageIndex < (gridView.PageCount - 1))
InBlock.gif                        gridView.PageIndex 
+= 1;
InBlock.gif                    
break;
InBlock.gif                
case "Page$Last":
InBlock.gif                    gridView.PageIndex 
= gridView.PageCount - 1;
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 一个新的LinkButton对象
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="text">文本</param>
InBlock.gif        
/// <param name="cmd">相应命令</param>
InBlock.gif        
/// <param name="css">Css风格</param>
InBlock.gif        
/// <param name="enabled">可用</param>
ExpandedSubBlockEnd.gif        
/// <returns>LinkButton</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) dot.gif{
InBlock.gif            LinkButton button 
= new LinkButton();
InBlock.gif            button.Text 
= text;
InBlock.gif            button.CommandName 
= cmd;
InBlock.gif            button.CssClass 
= css;
InBlock.gif            button.Enabled 
= enabled;
InBlock.gif            button.CausesValidation 
= false;
InBlock.gif            
return button;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/lyout/archive/2007/06/02/768970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值