(一). 概述
1. 当鼠标悬浮到控件对象上, 右击, 会弹出此自定义控件.
2. 仅当控件支持oncontextmenu事件. 另外, 与其功能类似的控件[浮动Toobar自定义控件]
(二). 运行如图
(三). 用法
1. 将PopupTraceMenur控件Dll添加引用到Web站点工程中
2. 从工具箱拖动一个PopupTraceMenu控件和另一个支持oncontextmenu事件的控件到设计器中,
比如: <GridView id="GridView1" runat="server" />
3. 设置PopupTraceMenur的属性TargetControl=GridView1
4. F5运行即可
(四). 完整代码
1. 主控件PopupTraceMenu.cs代码
2 /// Author: ChengKing(ZhengJian)
3 /// </summary>
4 [DefaultProperty( " TargetControl " )]
5 [ToolboxData( " <{0}:PopupTraceMenu runat=server></{0}:PopupTraceMenu> " )]
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 " <script type='text/javascript' src='JScript.js'></script> " );
70 }
71
72 Control targetControl = this .FindControl(TargetControl);
73
74 StringBuilder strInitScript = new StringBuilder();
75 if (targetControl != null )
76 {
77 strInitScript.Append( " <script text/javascript> " );
78 strInitScript.Append( " writeStyle(); makeMenu(); " );
79 strInitScript.Append( " if( document.all&&window.print ) " );
80 strInitScript.Append( " { " );
81 strInitScript.Append( " var objClientId = ' " + targetControl.ClientID + " '; " );
82 strInitScript.Append( " if(objClientId != null) " );
83 strInitScript.Append( " { " );
84 strInitScript.Append( " var obj = document.getElementById(objClientId); " );
85 strInitScript.Append( " document.onclick = hideMenu; " );
86 strInitScript.Append( " obj.oncontextmenu = showMenu; " );
87 strInitScript.Append( " } " );
88 strInitScript.Append( " else {alert('Please Set TargetControl Property!')} " );
89 strInitScript.Append( " } " );
90 strInitScript.Append( " </script> " );
91 }
92 else
93 {
94 strInitScript.Append( " <script text/javascript> " );
95 strInitScript.Append( " alert('Please Set TargetControl Property!'); " );
96 strInitScript.Append( " </script> " );
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代码
1 <div>
2 <asp:GridView ID="GridView1" runat="server" </asp:GridView>
3 <cc1:PopupTraceMenu ID="PopupTraceMenu1" runat="server" TargetControl= "GridView1">
</cc1:PopupTraceMenu>
4 </div>
(五). 示例代码下载
http://www.cnblogs.com/Files/MVP33650/自定义控件开发--[右键菜单].rar
(六). 控件开发其它相关文章:
http://blog.csdn.net/ChengKing/category/288694.aspx