首页
最近更新
热门教程
ASP.NET
开发语言
开发环境
AJax教程
控件开发
统计报表
数据库
Web服务
安装部署
HTML教程
Javascript
XML教程
Community Server
NHibernate
书籍推荐
常用工具
实用代码
教程全文搜索
首页
>>
控件开发
扩展GridView控件(九)——给数据行增加右键菜单
添加人:
iyond
添加时间:2007-03-29 阅读次数:2786
收藏此教程
介绍
给GridView的数据行增加右键菜单可以增加用户体验,不过实现起来挺麻烦的,现在我们扩展一下GridView控件以实现这样的功能。
控件开发
1、新建一个继承自GridView的类。
/**/
///
<summary>
///
继承自GridView
///
</summary>
[ToolboxData(
@"
<{0}:SmartGridView runat='server'></{0}:SmartGridView>
"
)]
public
class
SmartGridView : GridView
{
}
2、新建一个ContextMenu实体类,有六个属性
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.ComponentModel;
using
System.Web.UI;
namespace
YYControls.SmartGridView
{
/**/
///
<summary>
///
ContextMenu 的摘要说明。
///
</summary>
[ToolboxItem(
false
)]
public
class
ContextMenu
{
private
string
_icon;
/**/
///
<summary>
///
文字左边的图标的链接
///
</summary>
public
string
Icon
{
get
{
return
_icon; }
set
{ _icon
=
value; }
}
private
string
_text;
/**/
///
<summary>
///
菜单的文字
///
</summary>
public
string
Text
{
get
{
return
_text; }
set
{ _text
=
value; }
}
private
string
_commandButtonId;
/**/
///
<summary>
///
所调用的命令按钮的ID
///
</summary>
public
string
CommandButtonId
{
get
{
return
_commandButtonId; }
set
{ _commandButtonId
=
value; }
}
private
string
_navigateUrl;
/**/
///
<summary>
///
链接的url
///
</summary>
public
string
NavigateUrl
{
get
{
return
_navigateUrl; }
set
{ _navigateUrl
=
value; }
}
private
ItemTypeCollection _itemType;
/**/
///
<summary>
///
右键菜单的项的类别
///
</summary>
public
ItemTypeCollection ItemType
{
get
{
return
_itemType; }
set
{ _itemType
=
value; }
}
private
TargetCollection _target;
/**/
///
<summary>
///
链接的target
///
</summary>
public
TargetCollection Target
{
get
{
return
_target; }
set
{ _target
=
value; }
}
/**/
///
<summary>
///
右键菜单的项的类别
///
</summary>
public
enum
ItemTypeCollection
{
/**/
///
<summary>
///
链接
///
</summary>
Link,
/**/
///
<summary>
///
按钮
///
</summary>
Command,
/**/
///
<summary>
///
分隔线
///
</summary>
Separator
}
/**/
///
<summary>
///
链接的target
///
</summary>
public
enum
TargetCollection
{
/**/
///
<summary>
///
新开窗口
///
</summary>
Blank,
/**/
///
<summary>
///
当前窗口
///
</summary>
Self,
/**/
///
<summary>
///
跳出框架
///
</summary>
Top
}
}
}
3、新建一个继承自CollectionBase的类ContextMenus
using
System.Collections;
using
System.ComponentModel;
using
System.Web.UI;
namespace
YYControls.SmartGridView
{
/**/
///
<summary>
///
ContextMenus 的摘要说明。
///
注意要继承自CollectionBase
///
</summary>
[
ToolboxItem(
false
),
ParseChildren(
true
)
]
public
class
ContextMenus : CollectionBase
{
/**/
///
<summary>
///
构造函数
///
</summary>
public
ContextMenus()
:
base
()
{
}
/**/
///
<summary>
///
实现IList接口
///
获取或设置指定索引处的元素。
///
</summary>
///
<param name="index">
要获得或设置的元素从零开始的索引
</param>
///
<returns></returns>
public
ContextMenu
this
[
int
index]
{
get
{
return
(ContextMenu)
base
.List[index];
}
set
{
base
.List[index]
=
(ContextMenu)value;
}
}
/**/
///
<summary>
///
实现IList接口
///
将某项添加到 System.Collections.IList 中。
///
</summary>
///
<param name="item">
要添加到 System.Collections.IList 的 System.Object。
</param>
public
void
Add(ContextMenu item)
{
base
.List.Add(item);
}
/**/
///
<summary>
///
实现IList接口
///
从 System.Collections.IList 中移除特定对象的第一个匹配项。
///
</summary>
///
<param name="index">
要从 System.Collections.IList 移除的 System.Object
</param>
public
void
Remove(
int
index)
{
if
(index
>
-
1
&&
index
<
base
.Count)
{
base
.List.RemoveAt(index);
}
}
/**/
///
<summary>
///
ToString()
///
</summary>
///
<returns></returns>
public
override
string
ToString()
{
return
"
ContextMenus
"
;
}
}
}
4、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第3步创建的那个ContextMenus
private
ContextMenus _contextMenus;
/**/
///
<summary>
///
行的右键菜单集合
///
</summary>
[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Description(
"
行的右键菜单
"
),
Category(
"
扩展
"
)
]
public
virtual
ContextMenus ContextMenus
{
get
{
if
(_contextMenus
==
null
)
{
_contextMenus
=
new
ContextMenus();
}
return
_contextMenus;
}
}
5、新建一个JavaScriptConstant类,把我们要用到的javascript存在一个常量里
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
YYControls.SmartGridView
{
/**/
///
<summary>
///
javascript
///
</summary>
public
class
JavaScriptConstant
{
internal
const
string
jsContextMenu
=
@"
<script type=""text/javascript"">
//<![CDATA[
// 数据行的ClientId
var _rowClientId = '';
// 以下实现右键菜单,网上找的,不知道原创是谁
function contextMenu()
{
this.items = new Array();
this.addItem = function (item)
{
this.items[this.items.length] = item;
}
this.show = function (oDoc)
{
var strShow = '';
var i;
// 加上word-break: keep-all; 防止菜单项换行
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'>"";
strShow += ""<table border='0' height='"";
strShow += this.items.length * 20;
strShow += ""' cellpadding='0' cellspacing='0'>"";
strShow += ""<tr height='3'><td bgcolor='#d0d0ce' width='2'></td><td>"";
strShow += ""<table border='0' width='100%' height='100%' cellpadding=0 cellspacing=0 bgcolor='#ffffff'>"";
strShow += ""<tr><td bgcolor='#d0d0ce' width='23'></td><td><img src=' ' height='1' border='0'></td></tr></table>"";
strShow += ""</td><td width='2'></td></tr>"";
strShow += ""<tr><td bgcolor='#d0d0ce'></td><td>"";
strShow += ""<table border='0' width='100%' height='100%' cellpadding=3 cellspacing=0 bgcolor='#ffffff'>"";
oDoc.write(strShow);
for(i=0; i<this.items.length; i++)
{
this.items[i].show(oDoc);
}
strShow = ""</table></td><td></td></tr>"";
strShow += ""<tr height='3'><td bgcolor='#d0d0ce'></td><td>"";
strShow += ""<table border='0' width='100%' height='100%' cellpadding=0 cellspacing=0 bgcolor='#ffffff'>"";
strShow += ""<tr><td bgcolor='#d0d0ce' width='23'></td><td><img src=' ' height='1' border='0'></td></tr></table>"";
strShow += ""</td><td></td></tr>"";
strShow += ""</table></div>\n"";