(一). 概述
1. 当鼠标悬浮到控件对象上, 右击, 会弹出此自定义控件.
2. 仅当控件支持oncontextmenu事件.
(二). 运行如图
(三). 用法
1. 将PopupTraceMenur控件Dll添加引用到Web站点工程中
2. 从工具箱拖动一个PopupTraceMenu控件和另一个支持oncontextmenu事件的控件到设计器中,
比如:
3. 设置PopupTraceMenur的属性TargetControl=GridView1
4. F5运行即可
(四). 完整代码
1. 主控件PopupTraceMenu.cs代码
1
///
2
///
Author: ChengKing(ZhengJian)
3
///
4
[DefaultProperty(
"
TargetControl
"
)]
5
[ToolboxData(
"
<{0}:PopupTraceMenu runat=server>
"
)]
6
//
[Designer(typeof(System.ComponentModel.Design.DesignerCollection))]
7
public
class
PopupTraceMenu : Control, IComponent
8
{
9
10
[Bindable(
true
)]
11
[Category(
"
Appearance
"
)]
12
[DefaultValue(
"
[PopupTraceMenu \
"
PopupTraceMenu11\
"
]
"
)]
13
[Localizable(
true
)]
14
public
string
Text
15
{
16
get
17
{
18
String s
=
(String)ViewState[
"
Text
"
];
19
return
((s
==
null
)
?
String.Empty : s);
20
}
21
22
set
23
{
24
ViewState[
"
Text
"
]
=
value;
25
}
26
}
27
28
[Bindable(
true
)]
29
[Category(
"
Appearance
"
)]
30
[DefaultValue(
""
)]
31
[Description(
"
设置此控件的寄主控件
"
)]
32
[TypeConverter(
typeof
(ControlIDConverter))]
33
public
string
TargetControl
34
{
35
get
36
{
37
String s
=
(String)ViewState[
"
TargetControl
"
];
38
return
((s
==
null
)
?
String.Empty : s);
39
}
40
set
41
{
42
ViewState[
"
TargetControl
"
]
=
value;
43
}
44
}
45
46
protected
override
void
Render(HtmlTextWriter writer)
47
{
48
if
(DesignMode)
49
{
50
this
.Controls.Clear();
51
LiteralControl lc
=
new
LiteralControl();
52
lc.Text
=
this
.Text;
53
this
.Controls.Add(lc);
54
}
55
base
.Render(writer);
56
}
57
58
protected
override
void
CreateChildControls()
59
{
60
base
.CreateChildControls();
61
}
62
63
protected
override
void
OnPreRender(EventArgs e)
64
{
65
66
if
(
!
Page.ClientScript.IsClientScriptBlockRegistered(
"
BuildMenu
"
))
67
{
68
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"
BuildMenu
"
,
69
"
");
89 strInitScript.Append(" } ");
90 strInitScript.Append("");
91 }
92 else
93 {
94 strInitScript.Append("
");
95 strInitScript.Append(" alert('Please Set TargetControl Property!'); ");
96 strInitScript.Append("");
97 }
98 if (!Page.ClientScript.IsStartupScriptRegistered("InitScript"))
99 {
100 Page.ClientScript.RegisterStartupScript(this.GetType(), "InitScript",
101 strInitScript.ToString());
102 }
103
104 base.OnPreRender(e);
105 }
106 }
2. 测试页面文件default.aspx代码
(五). 示例代码下载
http://files.cnblogs.com/MVP33650/自定义控件开发--[右键菜单].rar