(一). 概述
1. 当鼠标悬浮到控件对象上, 会弹出此自定义控件.
2. 具有通用性,只要支持 onmouseenter/onmouseleave 等几个事件方法的目标控件都可以使用此控件.
3. 此控件与前面自定义控件[右击弹出菜单]控件有些类似, 区别是这个控件客户端JavaScript部分有些麻烦.
(二). 运行如图
(三). 用法
1. 将FloatTraceMenu控件Dll添加引用到Web站点工程中
2. 从工具箱拖动一个FloatTraceMenu控件和另一个支持onmouseenter/onmouseleave 等事件
的控件到设计器中, 比如: <GridView id="GridView1" runat="server" /> <img id="img' /> 等
3. 设置FloatTraceMenu的属性指向目标控件ID, 比如: TargetControl=GridView1
4. F5运行即可
(四). 完整代码
1. 主控件FloatTraceMenu.cs代码
1
///
<summary>
2
///
Author:[ChengKing(ZhengJian)]
3
///
Blog:Http://blog.csdn.net/ChengKing
4
///
Date:2007/4/3
5
///
</summary>
6
[DefaultProperty(
"
TargetControl
"
)]
7
[ToolboxData(
"
<{0}:FloatTraceMenurunat=server></{0}:FloatTraceMenu>
"
)]
8
//
[Designer(typeof(System.ComponentModel.Design.DesignerCollection))]
9
public
class
FloatTraceMenu:Control,IComponent
10
{
11
12
[Bindable(
true
)]
13
[Category(
"
Appearance
"
)]
14
[DefaultValue(
"
[FloatTraceMenu\
"
FloatTraceMenu1\
"
]
"
)]
15
[Localizable(
true
)]
16
public
string
Text
17
{
18
get
19
{
20
Strings
=
(String)ViewState[
"
Text
"
];
21
return
((s
==
null
)
?
String.Empty:s);
22
}
23
24
set
25
{
26
ViewState[
"
Text
"
]
=
value;
27
}
28
}
29
30
[Bindable(
true
)]
31
[Category(
"
Appearance
"
)]
32
[DefaultValue(
""
)]
33
[Description(
"
设置此控件的寄主控件
"
)]
34
[TypeConverter(
typeof
(ControlIDConverter))]
35
public
string
TargetControl
36
{
37
get
38
{
39
Strings
=
(String)ViewState[
"
TargetControl
"
];
40
return
((s
==
null
)
?
String.Empty:s);
41
}
42
set
43
{
44
ViewState[
"
TargetControl
"
]
=
value;
45
}
46
}
47
48
protected
override
void
Render(HtmlTextWriterwriter)
49
{
50
if
(DesignMode)
51
{
52
this
.Controls.Clear();
53
LiteralControllc
=
new
LiteralControl();
54
lc.Text
=
this
.Text;
55
this
.Controls.Add(lc);
56
}
57
base
.Render(writer);
58
}
59
60
protected
override
void
CreateChildControls()
61
{
62
base
.CreateChildControls();
63
}
64
65
protected
override
void
OnPreRender(EventArgse)
66
{
67
68
if
(
!
Page.ClientScript.IsClientScriptBlockRegistered(
"
BuildMenu
"
))
69
{
70
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"
BuildMenu
"
,
71
"
<scripttype='text/javascript'src='JScript.js'></script>
"
);
72
}
73
74
//
GridView增加FloatMenu
75
ControltargetControl
=
this
.FindControl(TargetControl);
76
77
//
给GridView头增加FloatMenu
78
//
ControltargetControl=this.FindControl(TargetControl).Controls[0].Controls[0];
79
80
StringBuilderstrInitScript
=
new
StringBuilder();
81
if
(targetControl
!=
null
)
82
{
83
strInitScript.Append(
"
<scripttext/javascript>
"
);
84
strInitScript.Append(
"
writeStyle();makeMenu();varobj;
"
);
85
strInitScript.Append(
"
if(document.all&&window.print)
"
);
86
strInitScript.Append(
"
{
"
);
87
strInitScript.Append(
"
varobjClientId='
"
+
targetControl.ClientID
+
"
';
"
);
88
strInitScript.Append(
"
if(objClientId!=null)
"
);
89
strInitScript.Append(
"
{
"
);
90
strInitScript.Append(
"
obj=document.getElementById(objClientId);
"
);
91
strInitScript.Append(
"
document.onclick=forcehideMenu;
"
);
92
//
strInitScript.Append("obj.onmouseover=showMenu;");
93
strInitScript.Append(
"
obj.onmouseenter=showMenu;
"
);
94
//
strInitScript.Append("obj.onmouseout=hideMenu;");
95
strInitScript.Append(
"
obj.onmouseleave=hideMenu;
"
);
96
strInitScript.Append(
"
obj.onmousemove=moveMenu;
"
);
97
strInitScript.Append(
"
obj.oncontextmenu=showMenu;
"
);
98
strInitScript.Append(
"
}
"
);
99
strInitScript.Append(
"
else{alert('PleaseSetTargetControlProperty!')}
"
);
100
strInitScript.Append(
"
}
"
);
101
strInitScript.Append(
"
</script>
"
);
102
}
103
else
104
{
105
strInitScript.Append(
"
<scripttext/javascript>
"
);
106
strInitScript.Append(
"
alert('PleaseSetTargetControlProperty!');
"
);
107
strInitScript.Append(
"
</script>
"
);
108
}
109
if
(
!
Page.ClientScript.IsStartupScriptRegistered(
"
InitScript
"
))
110
{
111
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"
InitScript
"
,
112
strInitScript.ToString());
113
}
114
115
base
.OnPreRender(e);
116
}
117
}
2. 测试页面文件default.aspx代码
1<div>
2<asp:GridViewID="GridView1"runat="server"</asp:GridView>
3<cc1:FloatTraceMenu ID="FloatTraceMenu1"runat="server"TargetControl= "GridView1">
</cc1:FloatTraceMenu>
4</div>
(五). 示例代码下载
http://www.cnblogs.com/Files/MVP33650/自定义控件开发--[浮动工具条].rar
[扩展]可以自定义(手动编程)浮动菜单的款项
http://www.cnblogs.com/Files/MVP33650/自定义控件开发--[浮动工具条--可手动编码自定义菜单项]V2.rar
(六). 控件开发其它相关文章:
http://blog.csdn.net/ChengKing/category/288694.aspx