介绍
给GridView的数据行增加右键菜单可以增加用户体验,不过实现起来挺麻烦的,现在我们扩展一下GridView控件以实现这样的功能。


控件开发
1、新建一个继承自GridView的类。
20070911102742152.gif 2007091110274266.gif /**/ /// <summary>
2007091110274226.gif
/// 继承自GridView
2007091110274216.gif
/// </summary>

2007091110274260.gif [ToolboxData( @" <{0}:SmartGridView runat='server'></{0}:SmartGridView> " )]
2007091110274260.gif
public   class  SmartGridView : GridView
20070911102742152.gif2007091110274266.gif
2007091110274211.gif {
2007091110274216.gif}

2、新建一个ContextMenu实体类,有六个属性
2007091110274260.gif using  System;
2007091110274260.gif
using  System.Collections.Generic;
2007091110274260.gif
using  System.Text;
2007091110274260.gif
2007091110274260.gif
using  System.ComponentModel;
2007091110274260.gif
using  System.Web.UI;
2007091110274260.gif
2007091110274260.gif
namespace  YYControls.SmartGridView
20070911102742152.gif2007091110274266.gif
2007091110274211.gif {
2007091110274252.gif20070911102742102.gif    
/**//// <summary>
2007091110274226.gif    
/// ContextMenu 的摘要说明。
2007091110274290.gif    
/// </summary>

2007091110274226.gif    [ToolboxItem(false)]
2007091110274226.gif    
public class ContextMenu
2007091110274252.gif20070911102742102.gif    
2007091110274211.gif{
2007091110274226.gif        
private string _icon;
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 文字左边的图标的链接
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public string Icon
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
get 2007091110274211.gifreturn _icon; }
2007091110274252.gif20070911102742102.gif            
set 2007091110274211.gif{ _icon = value; }
2007091110274290.gif        }

2007091110274226.gif
2007091110274226.gif        
private string _text;
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 菜单的文字
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public string Text
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
get 2007091110274211.gifreturn _text; }
2007091110274252.gif20070911102742102.gif            
set 2007091110274211.gif{ _text = value; }
2007091110274290.gif        }

2007091110274226.gif       
2007091110274226.gif        
private string _commandButtonId;
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 所调用的命令按钮的ID
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public string CommandButtonId
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
get 2007091110274211.gifreturn _commandButtonId; }
2007091110274252.gif20070911102742102.gif            
set 2007091110274211.gif{ _commandButtonId = value; }
2007091110274290.gif        }

2007091110274226.gif
2007091110274226.gif        
private string _navigateUrl;
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 链接的url
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public string NavigateUrl
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
get 2007091110274211.gifreturn _navigateUrl; }
2007091110274252.gif20070911102742102.gif            
set 2007091110274211.gif{ _navigateUrl = value; }
2007091110274290.gif        }

2007091110274226.gif
2007091110274226.gif        
private ItemTypeCollection _itemType;
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 右键菜单的项的类别
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public ItemTypeCollection ItemType
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
get 2007091110274211.gifreturn _itemType; }
2007091110274252.gif20070911102742102.gif            
set 2007091110274211.gif{ _itemType = value; }
2007091110274290.gif        }

2007091110274226.gif
2007091110274226.gif        
private TargetCollection _target;
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 链接的target
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public TargetCollection Target
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
get 2007091110274211.gifreturn _target; }
2007091110274252.gif20070911102742102.gif            
set 2007091110274211.gif{ _target = value; }
2007091110274290.gif        }

2007091110274226.gif
2007091110274226.gif
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 右键菜单的项的类别
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public enum ItemTypeCollection
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
/**//// <summary>
2007091110274226.gif            
/// 链接
2007091110274290.gif            
/// </summary>

2007091110274226.gif            Link,
2007091110274252.gif20070911102742102.gif            
/**//// <summary>
2007091110274226.gif            
/// 按钮
2007091110274290.gif            
/// </summary>

2007091110274226.gif            Command,
2007091110274252.gif20070911102742102.gif            
/**//// <summary>
2007091110274226.gif            
/// 分隔线
2007091110274290.gif            
/// </summary>

2007091110274226.gif            Separator
2007091110274290.gif        }

2007091110274226.gif
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 链接的target
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public enum TargetCollection
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif            
/**//// <summary>
2007091110274226.gif            
/// 新开窗口
2007091110274290.gif            
/// </summary>

2007091110274226.gif            Blank,
2007091110274252.gif20070911102742102.gif            
/**//// <summary>
2007091110274226.gif            
/// 当前窗口
2007091110274290.gif            
/// </summary>

2007091110274226.gif            Self,
2007091110274252.gif20070911102742102.gif            
/**//// <summary>
2007091110274226.gif            
/// 跳出框架
2007091110274290.gif            
/// </summary>

2007091110274226.gif            Top
2007091110274290.gif        }

2007091110274290.gif    }

2007091110274216.gif}

2007091110274260.gif

3、新建一个继承自CollectionBase的类ContextMenus
2007091110274260.gif using  System.Collections;
2007091110274260.gif
using  System.ComponentModel;
2007091110274260.gif
using  System.Web.UI;
2007091110274260.gif
2007091110274260.gif
namespace  YYControls.SmartGridView
20070911102742152.gif2007091110274266.gif
2007091110274211.gif {
2007091110274252.gif20070911102742102.gif    
/**//// <summary>
2007091110274226.gif    
/// ContextMenus 的摘要说明。
2007091110274226.gif    
/// 注意要继承自CollectionBase
2007091110274290.gif    
/// </summary>

2007091110274226.gif    [
2007091110274226.gif    ToolboxItem(
false),
2007091110274226.gif    ParseChildren(
true)
2007091110274226.gif    ]
2007091110274226.gif    
public class ContextMenus : CollectionBase
2007091110274252.gif20070911102742102.gif    
2007091110274211.gif{
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 构造函数
2007091110274290.gif        
/// </summary>

2007091110274226.gif        public ContextMenus()
2007091110274226.gif            : 
base()
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274290.gif        }

2007091110274226.gif
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 实现IList接口
2007091110274226.gif        
/// 获取或设置指定索引处的元素。
2007091110274226.gif        
/// </summary>
2007091110274226.gif        
/// <param name="index">要获得或设置的元素从零开始的索引</param>
2007091110274290.gif        
/// <returns></returns>

2007091110274226.gif        public ContextMenu this[int index]
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274226.gif            
get
2007091110274252.gif20070911102742102.gif            
2007091110274211.gif{
2007091110274226.gif                
return (ContextMenu)base.List[index];
2007091110274290.gif            }

2007091110274226.gif            
set
2007091110274252.gif20070911102742102.gif            
2007091110274211.gif{
2007091110274226.gif                
base.List[index] = (ContextMenu)value;
2007091110274290.gif            }

2007091110274290.gif        }

2007091110274226.gif
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 实现IList接口
2007091110274226.gif        
/// 将某项添加到 System.Collections.IList 中。
2007091110274226.gif        
/// </summary>
2007091110274290.gif        
/// <param name="item">要添加到 System.Collections.IList 的 System.Object。</param>

2007091110274226.gif        public void Add(ContextMenu item)
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274226.gif            
base.List.Add(item);
2007091110274290.gif        }

2007091110274226.gif
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// 实现IList接口
2007091110274226.gif        
/// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
2007091110274226.gif        
/// </summary>
2007091110274290.gif        
/// <param name="index">要从 System.Collections.IList 移除的 System.Object</param>

2007091110274226.gif        public void Remove(int index)
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274226.gif            
if (index > -1 && index < base.Count)
2007091110274252.gif20070911102742102.gif            
2007091110274211.gif{
2007091110274226.gif                
base.List.RemoveAt(index);
2007091110274290.gif            }

2007091110274290.gif        }

2007091110274226.gif
2007091110274252.gif20070911102742102.gif        
/**//// <summary>
2007091110274226.gif        
/// ToString()
2007091110274226.gif        
/// </summary>
2007091110274290.gif        
/// <returns></returns>

2007091110274226.gif        public override string ToString()
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274226.gif            
return "ContextMenus";
2007091110274290.gif        }

2007091110274290.gif    }

2007091110274216.gif}

2007091110274260.gif

4、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第3步创建的那个ContextMenus
2007091110274260.gif          private  ContextMenus _contextMenus;
20070911102742152.gif2007091110274266.gif        
/**/ /// <summary>
2007091110274226.gif        
/// 行的右键菜单集合
2007091110274216.gif        
/// </summary>

2007091110274260.gif         [
2007091110274260.gif        PersistenceMode(PersistenceMode.InnerProperty),
2007091110274260.gif        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
2007091110274260.gif        Description(
" 行的右键菜单 " ),
2007091110274260.gif        Category(
" 扩展 " )
2007091110274260.gif        ]
2007091110274260.gif        
public   virtual  ContextMenus ContextMenus
20070911102742152.gif2007091110274266.gif        
2007091110274211.gif {
2007091110274226.gif            
get
2007091110274252.gif20070911102742102.gif            
2007091110274211.gif{
2007091110274226.gif                
if (_contextMenus == null)
2007091110274252.gif20070911102742102.gif                
2007091110274211.gif{
2007091110274226.gif                    _contextMenus 
= new ContextMenus();
2007091110274290.gif                }

2007091110274226.gif                
return _contextMenus;
2007091110274290.gif            }

2007091110274216.gif        }

5、新建一个JavaScriptConstant类,把我们要用到的javascript存在一个常量里
2007091110274260.gif using  System;
2007091110274260.gif
using  System.Collections.Generic;
2007091110274260.gif
using  System.Text;
2007091110274260.gif
2007091110274260.gif
namespace  YYControls.SmartGridView
20070911102742152.gif2007091110274266.gif
2007091110274211.gif {
2007091110274252.gif20070911102742102.gif    
/**//// <summary>
2007091110274226.gif    
/// javascript
2007091110274290.gif    
/// </summary>

2007091110274226.gif    public class JavaScriptConstant
2007091110274252.gif20070911102742102.gif    
2007091110274211.gif{
2007091110274226.gif        
internal const string jsContextMenu = @"<script type=""text/javascript"">
2007091110274226.gif        //<![CDATA[
2007091110274226.gif        // 数据行的ClientId
2007091110274226.gif        var _rowClientId = '';
2007091110274226.gif
2007091110274226.gif        // 以下实现右键菜单,网上找的,不知道原创是谁
2007091110274226.gif        function contextMenu()
2007091110274226.gif        {
2007091110274226.gif            this.items = new Array();
2007091110274226.gif            this.addItem = function (item)
2007091110274226.gif            {
2007091110274226.gif                this.items[this.items.length] = item;
2007091110274226.gif            }
2007091110274226.gif
2007091110274226.gif            this.show = function (oDoc)
2007091110274226.gif            {
2007091110274226.gif                var strShow = '';
2007091110274226.gif                var i;
2007091110274226.gif
2007091110274226.gif                // 加上word-break: keep-all; 防止菜单项换行
2007091110274226.gif                strShow = ""<div id='rightmenu' style='word-break: keep-all;BACKGROUND-COLOR: #ffffff; BORDER: #000000 1px solid; LEFT: 0px; POSITION: absolute; TOP: 0px; VISIBILITY: hidden; Z-INDEX: 10'>"";
2007091110274226.gif                strShow += ""<table border='0' height='"";
2007091110274226.gif                strShow += this.items.length * 20;
2007091110274226.gif                strShow += ""' cellpadding='0' cellspacing='0'>"";
2007091110274226.gif                strShow += ""<tr height='3'><td bgcolor='#d0d0ce' width='2'></td><td>"";
2007091110274226.gif                strShow += ""<table border='0' width='100%' height='100%' cellpadding=0 cellspacing=0 bgcolor='#ffffff'>"";
2007091110274226.gif                strShow += ""<tr><td bgcolor='#d0d0ce' width='23'></td><td><img src=' ' height='1' border='0'></td></tr></table>"";
2007091110274226.gif                strShow += ""</td><td width='2'></td></tr>"";
2007091110274226.gif                strShow += ""<tr><td bgcolor='#d0d0ce'></td><td>"";
2007091110274226.gif                strShow += ""<table border='0' width='100%' height='100%' cellpadding=3 cellspacing=0 bgcolor='#ffffff'>"";
2007091110274226.gif                
2007091110274226.gif                oDoc.write(strShow);
2007091110274226.gif
2007091110274226.gif                for(i=0; i<this.items.length; i++)
2007091110274226.gif                {
2007091110274226.gif                    this.items[i].show(oDoc);
2007091110274226.gif                }
2007091110274226.gif                
2007091110274226.gif                strShow = ""</table></td><td></td></tr>"";
2007091110274226.gif                strShow += ""<tr height='3'><td bgcolor='#d0d0ce'></td><td>"";
2007091110274226.gif                strShow += ""<table border='0' width='100%' height='100%' cellpadding=0 cellspacing=0 bgcolor='#ffffff'>"";
2007091110274226.gif                strShow += ""<tr><td bgcolor='#d0d0ce' width='23'></td><td><img src=' ' height='1' border='0'></td></tr></table>"";
2007091110274226.gif                strShow += ""</td><td></td></tr>"";
2007091110274226.gif                strShow += ""</table></div>\n"";
2007091110274226.gif                
2007091110274226.gif                oDoc.write(strShow);
2007091110274226.gif            }
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function contextItem(text, icon, cmd, url, target, type)
2007091110274226.gif        {
2007091110274226.gif            this.text = text ? text : '';
2007091110274226.gif            this.icon = icon ? icon : '';
2007091110274226.gif            this.cmd = cmd ? cmd : '';
2007091110274226.gif            this.url = url ? url : '';
2007091110274226.gif            this.target =target ? target : '';
2007091110274226.gif            this.type = type ? type : 'Link';
2007091110274226.gif
2007091110274226.gif
2007091110274226.gif            this.show = function (oDoc)
2007091110274226.gif            {
2007091110274226.gif                var strShow = '';
2007091110274226.gif
2007091110274226.gif                if(this.type == 'Link' || this.type == 'Command')
2007091110274226.gif                {
2007091110274226.gif                    strShow += ""<tr "";
2007091110274226.gif                    strShow += ""οnmοuseοver=\""changeStyle(this, 'on');\"" "";
2007091110274226.gif                    strShow += ""οnmοuseοut=\""changeStyle(this, 'out');\"" "";
2007091110274226.gif
2007091110274226.gif                    if (this.type == 'Command')
2007091110274226.gif                    {
2007091110274226.gif                        // 右键菜单是按钮类型,调用所对应的按钮的click事件
2007091110274226.gif                        strShow += ""οnclick=\""document.getElementById("";
2007091110274226.gif                        strShow += ""_rowClientId + "";
2007091110274226.gif                        strShow += ""'_"";
2007091110274226.gif                        strShow += this.cmd;
2007091110274226.gif                        strShow += ""').click()"";
2007091110274226.gif                    }
2007091110274226.gif                    else
2007091110274226.gif                    {
2007091110274226.gif                        // 右键菜单是链接类型
2007091110274226.gif                        if (this.target == 'Top') this.target = 'top';
2007091110274226.gif                        if (this.target == 'Self') this.target = 'self';
2007091110274226.gif
2007091110274226.gif                        if (this.target == 'top' || this.target == 'self')
2007091110274226.gif                        {
2007091110274226.gif                            strShow += ""οnclick=\"""";
2007091110274226.gif                            strShow += this.target;
2007091110274226.gif                            strShow += "".location='"";
2007091110274226.gif                            strShow += this.url;
2007091110274226.gif                            strShow += ""'"";
2007091110274226.gif                        }
2007091110274226.gif                        else
2007091110274226.gif                        {
2007091110274226.gif                            strShow += ""οnclick=\""window.open('"";
2007091110274226.gif                            strShow += this.url;
2007091110274226.gif                            strShow += ""')"";
2007091110274226.gif                        }
2007091110274226.gif                    }   
2007091110274226.gif                    strShow += ""\"">"";
2007091110274226.gif                    strShow += ""<td class='ltdexit' width='16'>"";
2007091110274226.gif
2007091110274226.gif                    if (this.icon == '')
2007091110274226.gif                    {
2007091110274226.gif                        strShow += '&nbsp;';
2007091110274226.gif                    }
2007091110274226.gif                    else 
2007091110274226.gif                    {
2007091110274226.gif                        strShow += ""<img border='0' src='"";
2007091110274226.gif                        strShow += this.icon;
2007091110274226.gif                        strShow += ""' width='16' height='16' style='POSITION: relative'></img>"";
2007091110274226.gif                    }
2007091110274226.gif
2007091110274226.gif                    strShow += ""</td><td class='mtdexit'>"";
2007091110274226.gif                    strShow += this.text;
2007091110274226.gif                    strShow += ""</td><td class='rtdexit' width='5'>&nbsp;</td></tr>"";
2007091110274226.gif                }
2007091110274226.gif                // 右键菜单是分隔线
2007091110274226.gif                else if (this.type == 'Separator')
2007091110274226.gif                {
2007091110274226.gif                    strShow += ""<tr><td class='ltdexit'>&nbsp;</td>"";
2007091110274226.gif                    strShow += ""<td class='mtdexit' colspan='2'><hr color='#000000' size='1'></td></tr>"";
2007091110274226.gif                }
2007091110274226.gif
2007091110274226.gif                oDoc.write(strShow);
2007091110274226.gif            }
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function changeStyle(obj, cmd)
2007091110274226.gif        { 
2007091110274226.gif            if(obj)
2007091110274226.gif            {
2007091110274226.gif                try 
2007091110274226.gif                {
2007091110274226.gif                    var imgObj = obj.children(0).children(0);
2007091110274226.gif
2007091110274226.gif                    if(cmd == 'on') 
2007091110274226.gif                    {
2007091110274226.gif                        obj.children(0).className = 'ltdfocus';
2007091110274226.gif                        obj.children(1).className = 'mtdfocus';
2007091110274226.gif                        obj.children(2).className = 'rtdfocus';
2007091110274226.gif                        
2007091110274226.gif                        if(imgObj)
2007091110274226.gif                        {
2007091110274226.gif                            if(imgObj.tagName.toUpperCase() == 'IMG')
2007091110274226.gif                            {
2007091110274226.gif                                imgObj.style.left = '-1px';
2007091110274226.gif                                imgObj.style.top = '-1px';
2007091110274226.gif                            }
2007091110274226.gif                        }
2007091110274226.gif                    }
2007091110274226.gif                    else if(cmd == 'out') 
2007091110274226.gif                    {
2007091110274226.gif                        obj.children(0).className = 'ltdexit';
2007091110274226.gif                        obj.children(1).className = 'mtdexit';
2007091110274226.gif                        obj.children(2).className = 'rtdexit';
2007091110274226.gif
2007091110274226.gif                        if(imgObj)
2007091110274226.gif                        {
2007091110274226.gif                            if(imgObj.tagName.toUpperCase() == 'IMG')
2007091110274226.gif                            {
2007091110274226.gif                                imgObj.style.left = '0px';
2007091110274226.gif                                imgObj.style.top = '0px';
2007091110274226.gif                            }
2007091110274226.gif                        }
2007091110274226.gif                    }
2007091110274226.gif                }
2007091110274226.gif                catch (e) {}
2007091110274226.gif            }
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function showMenu(rowClientId)
2007091110274226.gif        {
2007091110274226.gif            _rowClientId = rowClientId;
2007091110274226.gif
2007091110274226.gif            var x, y, w, h, ox, oy;
2007091110274226.gif
2007091110274226.gif            x = event.clientX;
2007091110274226.gif            y = event.clientY;
2007091110274226.gif
2007091110274226.gif            var obj = document.getElementById('rightmenu');
2007091110274226.gif
2007091110274226.gif            if (obj == null)
2007091110274226.gif                return true;
2007091110274226.gif
2007091110274226.gif            ox = document.body.clientWidth;
2007091110274226.gif            oy = document.body.clientHeight;
2007091110274226.gif
2007091110274226.gif            if(x > ox || y > oy)
2007091110274226.gif                return false;
2007091110274226.gif
2007091110274226.gif            w = obj.offsetWidth;
2007091110274226.gif            h = obj.offsetHeight;
2007091110274226.gif
2007091110274226.gif            if((x + w) > ox)
2007091110274226.gif                x = x - w;
2007091110274226.gif
2007091110274226.gif            if((y + h) > oy)
2007091110274226.gif                y = y - h;
2007091110274226.gif
2007091110274226.gif         
2007091110274226.gif            // obj.style.posLeft = x + document.body.scrollLeft;
2007091110274226.gif            // obj.style.posTop = y + document.body.scrollTop;
2007091110274226.gif            // xhtml不支持上面的了
2007091110274226.gif            // 就是说如果你的页头声明了页是xhtml的话就不能用上面那句了,vs2005创建的aspx会默认加上xhtml声明
2007091110274226.gif            // 此时应该用如下的方法
2007091110274226.gif            obj.style.posLeft = x + document.documentElement.scrollLeft;
2007091110274226.gif            obj.style.posTop = y + document.documentElement.scrollTop;
2007091110274226.gif
2007091110274226.gif            obj.style.visibility = 'visible';
2007091110274226.gif
2007091110274226.gif            return false;
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function hideMenu()
2007091110274226.gif        {
2007091110274226.gif            if(event.button == 0)
2007091110274226.gif            {
2007091110274226.gif                var obj = document.getElementById('rightmenu');
2007091110274226.gif                if (obj == null)
2007091110274226.gif                    return true;
2007091110274226.gif
2007091110274226.gif                obj.style.visibility = 'hidden';
2007091110274226.gif                obj.style.posLeft = 0;
2007091110274226.gif                obj.style.posTop = 0;
2007091110274226.gif            }
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function writeStyle()
2007091110274226.gif        {
2007091110274226.gif            var strStyle = '';
2007091110274226.gif
2007091110274226.gif            strStyle += ""<STYLE type='text/css'>"";
2007091110274226.gif            strStyle += ""TABLE {Font-FAMILY: 'Tahoma','Verdana','宋体'; FONT-SIZE: 9pt}"";
2007091110274226.gif            strStyle += "".mtdfocus {BACKGROUND-COLOR: #ccccff; BORDER-BOTTOM: #000000 1px solid; BORDER-TOP: #000000 1px solid; CURSOR: hand}"";
2007091110274226.gif            strStyle += "".mtdexit {BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM: #ffffff 1px solid; BORDER-TOP: #ffffff 1px solid}"";
2007091110274226.gif            strStyle += "".ltdfocus {BACKGROUND-COLOR: #ccccff; BORDER-BOTTOM: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; CURSOR: hand}"";
2007091110274226.gif            strStyle += "".ltdexit {BACKGROUND-COLOR: #d0d0ce; BORDER-BOTTOM: #d0d0ce 1px solid; BORDER-TOP: #d0d0ce 1px solid; BORDER-LEFT: #d0d0ce 1px solid}"";
2007091110274226.gif            strStyle += "".rtdfocus {BACKGROUND-COLOR: #ccccff; BORDER-BOTTOM: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; CURSOR: hand}"";
2007091110274226.gif            strStyle += "".rtdexit {BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM: #ffffff 1px solid; BORDER-TOP: #ffffff 1px solid; BORDER-RIGHT: #ffffff 1px solid}"";
2007091110274226.gif            strStyle += ""</STYLE>"";
2007091110274226.gif
2007091110274226.gif            document.write(strStyle);
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function makeMenu()
2007091110274226.gif        {
2007091110274226.gif            var myMenu, item;
2007091110274226.gif
2007091110274226.gif            myMenu = new contextMenu();
2007091110274226.gif
2007091110274226.gif            // 增加右键菜单项 开始
2007091110274226.gif            // item = new contextItem("", "", "", "", "", "");
2007091110274226.gif            // 1-菜单项的文本
2007091110274226.gif            // 2-图标链接
2007091110274226.gif            // 3-所调用的命令按钮的ID
2007091110274226.gif            // 4-链接地址
2007091110274226.gif            // 5-链接的target
2007091110274226.gif            // 6-右键菜单的项的类别
2007091110274226.gif            // myMenu.addItem(item);
2007091110274226.gif
2007091110274226.gif            [$MakeMenu$]
2007091110274226.gif            // 增加右键菜单项 结束
2007091110274226.gif
2007091110274226.gif            myMenu.show(this.document);
2007091110274226.gif
2007091110274226.gif            delete item;
2007091110274226.gif
2007091110274226.gif            delete myMenu;
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        function toggleMenu(isEnable)
2007091110274226.gif        {
2007091110274226.gif            if(isEnable)
2007091110274226.gif                document.oncontextmenu = showMenu;
2007091110274226.gif            else
2007091110274226.gif                document.oncontextmenu = new function() {return true;};
2007091110274226.gif        }
2007091110274226.gif
2007091110274226.gif        writeStyle();
2007091110274226.gif
2007091110274226.gif        makeMenu();
2007091110274226.gif
2007091110274226.gif        document.onclick = hideMenu;
2007091110274226.gif        //]]>
2007091110274226.gif        </script>
";
2007091110274290.gif    }

2007091110274216.gif}

2007091110274260.gif

6、重写OnPreRender方法,注册上面那段客户端脚本
20070911102742152.gif 2007091110274266.gif          /**/ /// <summary>
2007091110274226.gif        
/// OnPreRender
2007091110274226.gif        
/// </summary>
2007091110274216.gif        
/// <param name="e"></param>

2007091110274260.gif          protected   override   void  OnPreRender(EventArgs e)
20070911102742152.gif2007091110274266.gif        
2007091110274211.gif {
2007091110274226.gif            
if (ContextMenus.Count > 0)
2007091110274252.gif20070911102742102.gif            
2007091110274211.gif{
2007091110274226.gif                StringBuilder sb 
= new StringBuilder();
2007091110274226.gif                
foreach (ContextMenu cm in ContextMenus)
2007091110274252.gif20070911102742102.gif                
2007091110274211.gif{
2007091110274226.gif                    
// item = new contextItem("", "", "", "", "", "");
2007091110274226.gif                    
// 1-菜单项的文本
2007091110274226.gif                    
// 2-图标链接
2007091110274226.gif                    
// 3-所调用的命令按钮的ID
2007091110274226.gif                    
// 4-链接地址
2007091110274226.gif                    
// 5-链接的target
2007091110274226.gif                    
// 6-右键菜单的项的类别
2007091110274226.gif
2007091110274226.gif                    
// 命令按钮
2007091110274226.gif
                    if (cm.ItemType == ContextMenu.ItemTypeCollection.Command)
2007091110274252.gif20070911102742102.gif                    
2007091110274211.gif{
2007091110274226.gif                        sb.Append(
"item = new contextItem(\"" + cm.Text +
2007091110274226.gif
                            "\", \"" + ResolveUrl(cm.Icon) + "\", \"" +
2007091110274226.gif                            cm.CommandButtonId 
+ "\", \"\", \"\", \"Command\");");
2007091110274290.gif
                    }

2007091110274226.gif                    
// 链接
2007091110274226.gif
                    else if (cm.ItemType == ContextMenu.ItemTypeCollection.Link)
2007091110274252.gif20070911102742102.gif                    
2007091110274211.gif{
2007091110274226.gif                        sb.Append(
"item = new contextItem(\"" + cm.Text +
2007091110274226.gif
                            "\", \"" + ResolveUrl(cm.Icon) + "\", \"\", \"" +
2007091110274226.gif                            cm.NavigateUrl 
+ "\", \"" +
2007091110274226.gif                            cm.Target 
+ "\", \"Link\");");
2007091110274290.gif
                    }

2007091110274226.gif                    
// 分隔线
2007091110274226.gif
                    else if (cm.ItemType == ContextMenu.ItemTypeCollection.Separator)
2007091110274252.gif20070911102742102.gif                    
2007091110274211.gif{
2007091110274226.gif                        sb.Append(
"item = new contextItem(\"\", \"\", \"\", \"\", \"\", \"Separator\");");
2007091110274290.gif                    }

2007091110274226.gif
2007091110274226.gif                    sb.Append(
"myMenu.addItem(item);");
2007091110274290.gif                }

2007091110274226.gif
2007091110274226.gif                
// 注册客户端代码
2007091110274226.gif
                if (!Page.ClientScript.IsClientScriptBlockRegistered("jsContextMenu"))
2007091110274252.gif20070911102742102.gif                
2007091110274211.gif{
2007091110274226.gif                    Page.ClientScript.RegisterClientScriptBlock(
2007091110274226.gif                        
this.GetType(),
2007091110274226.gif                        
"jsContextMenu", JavaScriptConstant.jsContextMenu.Replace("[$MakeMenu$]", sb.ToString())
2007091110274226.gif                        );
2007091110274290.gif                }

2007091110274290.gif            }

2007091110274226.gif
2007091110274226.gif            
base.OnPreRender(e);
2007091110274216.gif        }

2007091110274260.gif

7、重写OnRowDataBound给数据行增加客户端代码以调用我们注册的那段javascript,从而实现给GridView的数据行增加右键菜单的功能。
20070911102742152.gif 2007091110274266.gif          /**/ /// <summary>
2007091110274226.gif        
/// OnRowDataBound
2007091110274226.gif        
/// </summary>
2007091110274216.gif        
/// <param name="e"></param>

2007091110274260.gif          protected   override   void  OnRowDataBound(GridViewRowEventArgs e)
20070911102742152.gif2007091110274266.gif        
2007091110274211.gif {
2007091110274226.gif            
if (e.Row.RowType == DataControlRowType.DataRow)
2007091110274252.gif20070911102742102.gif            
2007091110274211.gif{
2007091110274226.gif                
// 给数据行增加客户端代码
2007091110274226.gif
                e.Row.Attributes.Add("oncontextmenu""showMenu('" + e.Row.ClientID + "');return false;");
2007091110274290.gif            }

2007091110274226.gif
2007091110274226.gif            
base.OnRowDataBound(e);
2007091110274216.gif        }

2007091110274260.gif


控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置如下属性:ItemType为右键菜单的项的类别(Link,Command,Separator);Icon为文字左边的图标的链接;Text为菜单的文字;CommandButtonId为所调用的命令按钮的ID;NavigateUrl为链接的url;Target为链接的target(Blank,Self,Top)
ObjData.cs
2007091110274260.gif using  System;
2007091110274260.gif
using  System.Data;
2007091110274260.gif
using  System.Configuration;
2007091110274260.gif
using  System.Web;
2007091110274260.gif
using  System.Web.Security;
2007091110274260.gif
using  System.Web.UI;
2007091110274260.gif
using  System.Web.UI.WebControls;
2007091110274260.gif
using  System.Web.UI.WebControls.WebParts;
2007091110274260.gif
using  System.Web.UI.HtmlControls;
2007091110274260.gif
2007091110274260.gif
using  System.ComponentModel;
2007091110274260.gif
20070911102742152.gif2007091110274266.gif
/**/ /// <summary>
2007091110274226.gif
/// OjbData 的摘要说明
2007091110274216.gif
/// </summary>

2007091110274260.gif public   class  OjbData
20070911102742152.gif2007091110274266.gif
2007091110274211.gif {
2007091110274226.gif    
public OjbData()
2007091110274252.gif20070911102742102.gif    
2007091110274211.gif{
2007091110274226.gif        
//
2007091110274226.gif        
// TODO: 在此处添加构造函数逻辑
2007091110274226.gif        
//
2007091110274290.gif
    }

2007091110274226.gif
2007091110274226.gif    [DataObjectMethod(DataObjectMethodType.Select, 
true)]
2007091110274226.gif    
public DataTable Select()
2007091110274252.gif20070911102742102.gif    
2007091110274211.gif{
2007091110274226.gif        DataTable dt 
= new DataTable();
2007091110274226.gif        dt.Columns.Add(
"no"typeof(string));
2007091110274226.gif        dt.Columns.Add(
"name"typeof(string));
2007091110274226.gif
2007091110274226.gif        
for (int i = 0; i < 30; i++)
2007091110274252.gif20070911102742102.gif        
2007091110274211.gif{
2007091110274226.gif            DataRow dr 
= dt.NewRow();
2007091110274226.gif            dr[
0= "no" + i.ToString().PadLeft(2'0');
2007091110274226.gif            dr[
1= "name" + i.ToString().PadLeft(2'0');
2007091110274226.gif
2007091110274226.gif            dt.Rows.Add(dr);
2007091110274290.gif        }

2007091110274226.gif
2007091110274226.gif        
return dt;
2007091110274290.gif    }

2007091110274216.gif}

2007091110274260.gif

Default.aspx
20070911102742152.gif 2007091110274266.gif <% 2007091110274211.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  %>
2007091110274260.gif
2007091110274260.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
2007091110274260.gif
< html  xmlns ="http://www.w3.org/1999/xhtml" >
2007091110274260.gif
< head  runat ="server" >
2007091110274260.gif    
< title > SmartGridView测试 </ title >
2007091110274260.gif
</ head >
2007091110274260.gif
< body >
2007091110274260.gif    
< form  id ="form1"  runat ="server" >
2007091110274260.gif        
< div >
2007091110274260.gif            
< yyc:SmartGridView  ID ="SmartGridView1"  runat ="server"  DataSourceID ="ObjectDataSource1"
2007091110274260.gif                AutoGenerateColumns
="false" >
2007091110274260.gif                
< Columns >
2007091110274260.gif                    
< asp:BoundField  DataField ="no"  HeaderText ="序号"  SortExpression ="no"  ItemStyle-Width ="100px"   />
2007091110274260.gif                    
< asp:BoundField  DataField ="name"  HeaderText ="名称"  SortExpression ="name"  ItemStyle-Width ="100px"   />
2007091110274260.gif                    
< asp:BoundField  DataField ="no"  HeaderText ="序号"  SortExpression ="no"  ItemStyle-Width ="100px"   />
2007091110274260.gif                    
< asp:BoundField  DataField ="name"  HeaderText ="名称"  SortExpression ="name"  ItemStyle-Width ="100px"   />
2007091110274260.gif                    
< asp:BoundField  DataField ="no"  HeaderText ="序号"  SortExpression ="no"  ItemStyle-Width ="100px"   />
2007091110274260.gif                    
< asp:BoundField  DataField ="name"  HeaderText ="名称"  SortExpression ="name"  ItemStyle-Width ="100px"   />
2007091110274260.gif                    
< asp:TemplateField >
2007091110274260.gif                        
< footerstyle  cssclass ="hidden"   />
2007091110274260.gif                        
< headerstyle  cssclass ="hidden"   />
2007091110274260.gif                        
< itemstyle  cssclass ="hidden"   />
2007091110274260.gif                        
< itemtemplate >
2007091110274260.gif                    
< asp:Button  id ="btnRightMenuButton"  runat ="server"  CommandName ="RightMenuButton"  CommandArgument ='<%#  Container.DataItemIndex % > ' />
2007091110274260.gif                
</ itemtemplate >
2007091110274260.gif                    
</ asp:TemplateField >
2007091110274260.gif                
</ Columns >
2007091110274260.gif                
< ContextMenus >
2007091110274260.gif                    
< yyc:ContextMenu  ItemType ="Command"  Text ="右键菜单按钮测试"  Icon ="~/Images/button.gif"  CommandButtonId ="btnRightMenuButton"   />
2007091110274260.gif                    
< yyc:ContextMenu  ItemType ="Separator"   />
2007091110274260.gif                    
< yyc:ContextMenu  ItemType ="Link"  Text ="控件源代码"  Icon ="~/Images/button.gif"  NavigateUrl ="http://webabcd.cnblogs.com"
2007091110274260.gif                        Target
="Blank"   />
2007091110274260.gif                
</ ContextMenus >
2007091110274260.gif            
</ yyc:SmartGridView >
2007091110274260.gif            
< asp:ObjectDataSource  ID ="ObjectDataSource1"  runat ="server"  SelectMethod ="Select"
2007091110274260.gif                TypeName
="OjbData" ></ asp:ObjectDataSource >
2007091110274260.gif        
</ div >
2007091110274260.gif    
</ form >
2007091110274260.gif
</ body >
2007091110274260.gif
</ html >
2007091110274260.gif

注:如果想修改右键菜单的样式,请自行修改javascript,我就不把他们弄出来了。


OK