原文:http://blog.csdn.net/ChengKing/archive/2008/04/05/2253619.aspx
Asp.net 2.0 自定义控件开发[工具栏菜单控件](示例代码下载) 收藏
.
.
(一). 概述
控件名称: 工具栏控件, 分为主菜单和子菜单部分, 涉及到公司代码版权, 目前版本不是最终版本, 且去除了一些代码, 后面可下载的仅为纯控件开发技术相关代码. 通过扩展可以任意定制需要的功能. 里面一些设计思想也具有参考价值.
Author:【夜战鹰】【ChengKing(ZhengJian)】
(二). 控件运行效果截图
1. 默认样式和自定义样式
2. 项集合编辑器
(三). 代码部分
1. 主控件类ToolButton代码
1 /// <summary>
2 /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// </summary>
5
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Text;
10 using System.Web;
11 using System.Web.UI;
12 using System.Web.UI.WebControls;
13 using System.Web.UI.HtmlControls;
14 using System.IO;
15 using System.Drawing;
16
17 namespace ToolButton
18 {
19 [DefaultProperty("Text")]
20 [ToolboxData("<{0}:ToolButton runat=server></{0}:ToolButton>")]
21 public class ToolButton : CompositeControl, INamingContainer
22 {
23 #region 内部变量
24
25 //含下拉箭头单元格的ID
26 private string tcDownID = "";
27
28 //本控件的ID
29 private string tbID = "";
30
31 #endregion
32
33 #region 属性
34
35 /// <summary>
36 /// 设置或获取显示的文本
37 /// </summary>
38 [Bindable(true)]
39 [Category("Appearance")]
40 [DefaultValue("[按钮文本]")]
41 [Localizable(true)]
42 [Description("显示的文本")]
43 public string Text
44 {
45 get
46 {
47 string s = (string)ViewState["Text"];
48 return ((s == null) ? String.Empty : s);
49 }
50 set
51 {
52 ViewState["Text"] = value;
53 }
54 }
55
56 /// <summary>
57 /// 设置或获取图片路径
58 /// </summary>
59 [Bindable(true)]
60 [Category("Appearance")]
61 [DefaultValue("NoPic.gif")]
62 [Localizable(true)]
63 [Description("显示图像的文件名")]
64 public string ImageName
65 {
66 get
67 {
68 String s = (String)ViewState["ImageName"];
69 return ((s == null) ? String.Empty : s);
70 }
71
72 set
73 {
74 ViewState["ImageName"] = value;
75 }
76 }
77
78 /// <summary>
79 /// 设置或获取图片文件夹目录
80 /// </summary>
81 [Bindable(true)]
82 [Category("客户端路径")]
83 [DefaultValue("")]
84 [Localizable(true)]
85 [Description("资源(image/css/js)的客户端根目录")]
86 public string ClientPath
87 {
88 get
89 {
90 String s = (String)ViewState["ClientPath"];
91 return ((s == null) ? String.Empty : s);
92 }
93
94 set
95 {
96 ViewState["ClientPath"] = value;
97 }
98 }
99
100 /// <summary>
101 /// 设置或获取点击按钮执行的命令(点击大按钮执行的命令)
102 /// </summary>
103 [Bindable(true)]
104 [Category("行为")]
105 [DefaultValue("")]
106 [Localizable(true)]
107 [Description("点击控件执行的命令字串")]
108 public string Command
109 {
110 get
111 {
112 string s = (string)ViewState["Command"];
113 return ((s == null) ? String.Empty : s);
114 }
115 set
116 {
117 ViewState["Command"] = value;
118 }
119 }
120
121 private PopupMenu _Items = new PopupMenu();
122
123 /// <summary>
124 /// 获取子菜单集合
125 /// </summary>
126 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
127 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
128 [Description("获取或设置下拉子菜单项集合")]
129 [Category("杂项")]
130 [NotifyParentProperty(true)]
131 [TypeConverter(typeof(CollectionConverter))]
132 [DesignOnly(false)]
133 public PopupMenu Items
134 {
135 get
136 {
137 if (_Items == null)
138 {
139 _Items = new PopupMenu();
140 }
141 return _Items;
142 }
143 }
144
145 /// <summary>
146 /// 设置或获取子菜单集合句柄
147 /// </summary>
148 [Browsable(false)]
149 [Description("获取或设置菜单")]
150 public PopupMenu Menu
151 {
152 get
153 {
154 return _Items;
155 }
156 set
157 {
158 this._Items.Clear();
159 foreach (PopupMenuItem item in value)
160 {
161 this._Items.Add(item);
162 }
163 }
164 }
165
166 [Browsable(true)]
167 [Category("CSS样式")]
168 [Description("获取或设置主控件的OnMouseOver样式")]
169 public string CSS_MAIN_ONMOUSEOVER_CLASSNAME
170 {
171 get
172 {
173 string s = (string)ViewState["CSS_MAIN_ONMOUSEOVER"];
174 return ((s == null) ? String.Empty : s);
175 }
176 set
177 {
178 ViewState["CSS_MAIN_ONMOUSEOVER"] = value;
179 }
180 }
181 [Browsable(true)]
182 [Category("CSS样式")]
183 [Description("获取或设置主控件的OnMouseOut样式")]
184 public string CSS_MAIN_ONMOUSEOUT_CLASSNAME
185 {
186 get
187 {
188 string s = (string)ViewState["CSS_MAIN_ONMOUSEOUT"];
189 return ((s == null) ? String.Empty : s);
190 }
191 set
192 {
193 ViewState["CSS_MAIN_ONMOUSEOUT"] = value;
194 }
195 }
196 [Browsable(true)]
197 [Category("CSS样式")]
198 [Description("获取或设置下拉款项的OnMouseOver样式")]
199 public string CSS_SUBITEM_ONMOUSEOVER_CLASSNAME
200 {
201 get
202 {
203 string s = (string)ViewState["CSS_SUBITEM_ONMOUSEOVER"];
204 return ((s == null) ? String.Empty : s);
205 }
206 set
207 {
208 ViewState["CSS_SUBITEM_ONMOUSEOVER"] = value;
209 }
210 }
211 [Browsable(true)]
212 [Category("CSS样式")]
213 [Description("获取或设置下拉款项的OnMouseOut样式")]
214 public string CSS_SUBITEM_ONMOUSEOUT_CLASSNAME
215 {
216 get
217 {
218 string s = (string)ViewState["CSS_SUBITEM_ONMOUSEOUT"];
219 return ((s == null) ? String.Empty : s);
220 }
221 set
222 {
223 ViewState["CSS_SUBITEM_ONMOUSEOUT"] = value;
224 }
225 }
226
227
228 #endregion
229
230 #region 构造函数
231
232 /// <summary>
233 /// 构造函数
234 /// </summary>
235 public ToolButton()
236 {
237 //设置一套默认风格
238 this.BackColor = Color.FromName("MenuBar");//ViewState["BackColor"] = Color.FromName("MenuBar");
239 ViewState["Text"] = "[按钮文本]";
240 //ViewState["ClientPath"] = "ToolButtonImages";
241 ViewState["ImageName"] = "NoPic.gif";
242 this.BorderStyle = BorderStyle.NotSet;//ViewState["BorderStyle"] = BorderStyle.NotSet;
243 this.BorderWidth = Unit.Pixel(1);//ViewState["BorderWidth"] = Unit.Pixel(1);
244 this.BorderColor = Color.FromName("ControlDark");//ViewState["BorderColor"] = Color.FromName("ControlDark");
245 this.Height = Unit.Pixel(25);//ViewState["Height"] = Unit.Pixel(25);
246 //this.Width = Unit.Pixel(100);]
247 }
248
249 #endregion
250
251 #region 方法
252
253 protected override void Render(HtmlTextWriter writer)
254 {
255 //base.Render(writer);
256 PrepareControlForRendering();
257 base.RenderContents(writer);
258 }
259
260 /// <summary>
261 /// 设置控件树的样式和客户端事件
262 /// </summary>
263 private void PrepareControlForRendering()
264 {
265
266 if (this.Controls.Count < 1)
267 {
268 return;
269 }
270
271 bool IsCustomStyle = CSS_MAIN_ONMOUSEOVER_CLASSNAME != String.Empty && CSS_MAIN_ONMOUSEOUT_CLASSNAME != String.Empty;
272
273 Table t = (Table)this.Controls[0];
274 t.CellPadding = 0;
275 t.CellSpacing = 0;
276 t.Style.Add("Cursor", "default");
277 t.CopyBaseAttributes(this);
278 t.Height = this.Height;
279 t.Width = this.Width;
280 t.BorderStyle = this.BorderStyle;
281 t.BorderWidth = Unit.Pixel(0);
282 t.Attributes.Add("onselectstart", "return false;");
283
284 //t.Style.Add("aligh", "left");
285 //t.Style.Add("table-layout", "fixed");
286 //t.Style.Add("word-wrap", "break-word");
287 //***style="display:inline-block
288 //t.Style.Add("display:inline", "block");
289
290 //if (CSS_MAIN_ONMOUSEOVER_CLASSNAME != String.Empty && CSS_MAIN_ONMOUSEOUT_CLASSNAME != String.Empty)
291 //{
292 // t.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
293 // t.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "')");
294 // t.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "')");
295 //}
296
297
298 if (t.Rows.Count > 0)
299 {
300 TableRow tr = t.Rows[0];
301 //if (IsCustomStyle == false)
302 //{
303 tr.BorderWidth = Unit.Pixel(0);
304 // }
305
306
307 ////应用用户定义样式
308 //if (IsCustomStyle)
309 //{
310 // tr.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
311 // tr.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "')");
312 // tr.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "')");
313 //}
314
315 for (int i = 0; i < tr.Cells.Count; i++)
316 {
317 TableCell tc = tr.Cells[i];
318 //tc.Style.Add("align", "left");
319 if (IsCustomStyle == false)
320 {
321 tc.BorderColor = this.BorderColor;
322 tc.BorderStyle = this.BorderStyle;
323 }
324
325 string strBorderWidth = int.Parse(this.BorderWidth.Value.ToString()).ToString();
326
327 //应用用户定义样式
328 if (IsCustomStyle)
329 {
330 tc.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
331 }
332 else
333 {
334 tc.CssClass = this. CssClass;
335 }
336
337
338 if (i == 0)
339 {
340 if (IsCustomStyle == false)
341 {
342 tc.BorderWidth = this.BorderWidth;
343 tc.Style.Add("border-Width", strBorderWidth + "px 0px " + strBorderWidth + "px " + strBorderWidth + "px");
344 }
345 else
346 {
347 tc.Style.Add("border-Right-Width", "0px");
348 //固定Border-width
349 //tc.Style.Add("border-Width", "1px 0px 1px 1px");
350 }
351
352 if (this.Command != null && this.Command.Length > 0)
353 {
354 string strCommand = this.Command.Trim().Replace("\"", "'").Replace(" ", "");
355 tc.Attributes.Add("onclick", strCommand);
356
357 if (IsCustomStyle == false)
358 {
359 string str = Color.FromName("LightGray").ToString();
360 tc.Attributes.Add("onmouseenter", "this.style.borderStyle='inset'; this.parentElement.children(1).style.borderStyle='inset';");
361 tc.Attributes.Add("onmouseleave", "this.style.borderStyle='outset';this.parentElement.children(1).style.borderStyle='outset';");
362 }
363 else
364 {
365 tc.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(1), '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');");
366 tc.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(1), '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');");
367 }
368 }
369
370 //tc.Style.Add("border-color", "red");
371 //tc.Style.Add("width", ((System.Web.UI.WebControls.Image)tc.Controls[0]).Width.ToString());
372 }
373 else if (i == 1)
374 {
375 if (IsCustomStyle == false)
376 {
377 tc.BorderWidth = this.BorderWidth;
378 tc.Style.Add("border-Width", strBorderWidth + "px 0px " + strBorderWidth + "px 0px");
379 }
380 else
381 {
382 tc.Style.Add("border-Left-Width", "0px");
383 //固定Border-width
384 //tc.Style.Add("border-Width", "1px 1px 1px 0px");
385 }
386
387 if (this.Command != null && this.Command.Length > 0)
388 {
389 string strCommand = this.Command.Trim().Replace("\"", "'").Replace(" ", "");
390 tc.Attributes.Add("onclick", strCommand);
391 if (IsCustomStyle == false)
392 {
393 tc.Attributes.Add("onmouseenter", "this.style.borderStyle='inset'; this.parentElement.children(0).style.borderStyle='inset';");
394 tc.Attributes.Add("onmouseleave", "this.style.borderStyle='outset';this.parentElement.children(0).style.borderStyle='outset';");
395 // tc.CssClass = "mainexit";
396 // tc.Attributes.Add("onmouseenter", "ChangeClassName(this, 'mainfocus');ChangeClassName(this.parentElement.children(0),'mainfocus');");
397 // tc.Attributes.Add("onmouseleave", "ChangeClassName(this, 'mainexit');ChangeClassName(this.parentElement.children(0),'mainexit');");
398 }
399 else
400 {
401 tc.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(0), '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');");
402 tc.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(0), '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');");
403 }
404 }
405
406 if (IsCustomStyle == false)
407 {
408 tc.Font.MergeWith(this.Font);
409 }
410 }
411 else if (i == 2)
412 {
413 if (IsCustomStyle == false)
414 {
415 tc.BorderWidth = this.BorderWidth;
416 }
417 else
418 {
419 //固定Border-width
420 //tc.Style.Add("border-Width","1px 1px 1px 1px");
421 }
422 if (IsCustomStyle == true)
423 {
424 tc.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');");
425 tc.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');");
426 }
427 else
428 {
429 tc.Attributes.Add("onmouseenter", "this.style.borderStyle='inset';");
430 tc.Attributes.Add("onmouseleave", "this.style.borderStyle='outset';");
431 }
432 //tc.Style.Add("border-color", "red");
433 }
434
435 if (IsCustomStyle == false)
436 {
437 tc.ForeColor = this.ForeColor;
438 tc.BackColor = this.BackColor;
439 }
440
441 if (tc.ID == tcDownID)
442 {
443 if (this.Items.Count > 0)
444 {
445 tc.Attributes.Add("onclick", "activeMenu('" + this.UniqueID + "');");
446 }
447 else
448 {
449 tc.Enabled = false;
450 //tc.Style.Add("display", "none");
451 if (tc.Parent.Controls.Count >= 2)
452 {
453 ((TableCell)tc.Parent.Controls[1]).Style.Add("border-Right-Width", "1px");
454 }
455 }
456 }
457 }
458 }
459 }
460
461 /// <summary>
462 /// 创建子控件的层次结构
463 /// </summary>
464 protected override void CreateChildControls()
465 {
466 Table t = new Table();
467 tbID = this.UniqueID + "_table";
468 t.ID = tbID;
469
470 TableRow tr = new TableRow();
471 t.Rows.Add(tr);
472
473 TableCell tc_img = new TableCell();
474 if (this.ClientPath != String.Empty || this.ImageName != String.Empty)
475 {
476 string strImageUrl = Path.Combine(this.ClientPath, "ToolButtonImages\\" + this.ImageName);
477 System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
478 image.BorderWidth = Unit.Pixel(0);
479 image.ImageUrl = strImageUrl;
480 tc_img.Controls.Add(image);
481 tr.Cells.Add(tc_img);
482 }
483
484 TableCell tc_text = new TableCell();
485 tc_text.Controls.Add(new LiteralControl(" " + this.Text + " "));
486 tr.Cells.Add(tc_text);
487
488 TableCell tc_downmenu = new TableCell();
489 tcDownID = this.UniqueID + "tcDown";
490 tc_downmenu.ID = tcDownID;
491 tc_downmenu.Controls.Add(new LiteralControl("<font size=1>▼</font>"));
492 tr.Cells.Add(tc_downmenu);
493
494 this.Controls.Add(t);
495 base.CreateChildControls();
496 }
497
498 //protected override void RenderContents(HtmlTextWriter writer)
499 //{
500 // base.RenderContents(writer);
501 //}
502
503 /// <summary>
504 /// 注册客户端脚本
505 /// </summary>
506 /// <param name="e"></param>
507 protected override void OnPreRender(EventArgs e)
508 {
509
510 //引用JS文件
511 if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "BuildMenu"))
512 {
513 //string jsPath = clientPath + "/Js/" + "jsscript.js";
514 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "BuildMenu",
515 "<script type='text/javascript' src='js/ToolButton_JScript.js'></script>");
516 }
517
518
519 ///<summary>
520 /// 注册makeMenu方法脚本
521 ///<summary>
522 StringBuilder strAddMenuItemScript = new StringBuilder();
523 if (this.Items.Count != 0)
524 {
525 //if (this.CSS_MAIN_ONMOUSEOUT_CLASSNAME != String.Empty)
526 //{
527
528 // TableCell tc = new TableCell();
529 // tc.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
530 // Color c = tc.BorderColor;
531 //}
532
533 strAddMenuItemScript.Append(" function makeMenu"+this.UniqueID+"(UniqueID)");
534 strAddMenuItemScript.Append(" { ");
535 strAddMenuItemScript.Append(" var myMenu, item; ");
536 strAddMenuItemScript.Append(" ");
537 strAddMenuItemScript.Append(" ");
538 strAddMenuItemScript.Append(" myMenu = new contextMenu(UniqueID); ");
539 strAddMenuItemScript.Append(" ");
540 foreach (PopupMenuItem item in this.Items)
541 {
542 string strImageSrc;
543 strImageSrc = Path.Combine(this.ClientPath, @"ToolButtonImages\" + item.ImageName);
544 strImageSrc = strImageSrc.Replace("\\", "\\\\");
545
546 string strCommand;
547 try
548 {
549 strCommand = item.Command.Trim().Replace("\"", "'").Replace(" ", "");
550 }
551 catch
552 {
553 strCommand = null;
554 }
555
556 strAddMenuItemScript.Append(" item = new contextItem('" + (item.Text == null ? " " : item.Text) + "','" + strImageSrc + "',\"" + (strCommand == null ? "" : (strCommand + "; hideCurrentMenu()")) + "\",'" + "Menu" + "','" + this.CSS_SUBITEM_ONMOUSEOVER_CLASSNAME + "','" + this.CSS_SUBITEM_ONMOUSEOUT_CLASSNAME + "');");
557 strAddMenuItemScript.Append(" myMenu.addItem(item); ");
558
559 }
560 strAddMenuItemScript.Append(" ");
561 strAddMenuItemScript.Append(" myMenu.show(this.document);");
562 strAddMenuItemScript.Append(" ");
563 strAddMenuItemScript.Append(" delete item;");
564 strAddMenuItemScript.Append(" delete myMenu;");
565 strAddMenuItemScript.Append(" }");
566 }
567 else
568 {
569 strAddMenuItemScript.Append(" function makeMenu"+this.UniqueID+"(UniqueID)");
570 strAddMenuItemScript.Append(" { ");
571 //strAddMenuItemScript.Append(" alert('No Set Items Property!');");
572 strAddMenuItemScript.Append(" }");
573 }
574
575 if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "AddMenuItemScript"+this.UniqueID))
576 {
577 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "AddMenuItemScript"+this.UniqueID, strAddMenuItemScript.ToString(), true);
578 }
579
580 /////<summary>
581 ///// 注册contextMenu方法脚本
582 /////<summary>
583 //StringBuilder strCreateDiv = new StringBuilder();
584 //strCreateDiv.Append("function contextMenu" + this.UniqueID + "()");
585 //strCreateDiv.Append("{\r\n");
586 //strCreateDiv.Append("this.items = new Array();\r\n");
587 //strCreateDiv.Append("this.addItem = function(item)\r\n");
588 //strCreateDiv.Append("{\r\n");
589 //strCreateDiv.Append(" this.items[this.items.length] = item;\r\n");
590 //strCreateDiv.Append("}\r\n");
591 //strCreateDiv.Append("this.show = function (oDoc)\r\n");
592 //strCreateDiv.Append("{\r\n");
593 //strCreateDiv.Append(" var strShow = \"\";\r\n");
594 //strCreateDiv.Append(" var i;\r\n");
595 //strCreateDiv.Append(" strShow = \"<div id='rightmenu" + this.UniqueID + "' onclick='event.cancelBubble=true;return true;' style='BACKGROUND-COLOR: system; BORDER: #000000 1px solid; LEFT: 0px; POSITION: absolute; TOP: 0px; display: none; Z-INDEX: 10'>\"; \r\n");
596 //strCreateDiv.Append(" strShow += \"<table border='0' height='\"; \r\n");
597 //strCreateDiv.Append(" strShow += this.items.length * 15;\r\n");
598 //strCreateDiv.Append(" strShow += \"' cellpadding='0' cellspacing='0'>\";\r\n");
599 //strCreateDiv.Append(" oDoc.write(strShow);\r\n");
600 //strCreateDiv.Append(" for(i=0; i<this.items.length; i++)\r\n");
601 //strCreateDiv.Append(" {\r\n");
602 //strCreateDiv.Append(" this.items[i].show(oDoc);\r\n");
603 //strCreateDiv.Append(" }\r\n");
604 //strCreateDiv.Append(" strShow += \"</table>\";\r\n");
605 //strCreateDiv.Append(" strShow += \"</div>\";\r\n");
606 //strCreateDiv.Append(" oDoc.write(strShow);\r\n");
607 //strCreateDiv.Append(" }\r\n");
608 //strCreateDiv.Append("}\r\n");
609 //if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "CreateMainDiv" + this.UniqueID))
610 //{
611 // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateMainDiv" + this.UniqueID, strCreateDiv.ToString(), true);
612 //}
613
614 ///<summary>
615 /// 注册contextMenu方法脚本
616 ///<summary>
617 // StringBuilder strChangeStyle = new StringBuilder();strChangeStyle.Append("
618
619
620
621
622 ///<summary>
623 ///添加StartUp脚本
624 ///<summary>
625 Control tcDown = this.FindControl(tcDownID);
626 Control table = this.FindControl(tbID);
627 StringBuilder strInitScript = new StringBuilder();
628 strInitScript.Append("<script text/javascript> ");
629 strInitScript.Append(" var tbClientID" + this.UniqueID + "='" + table.ClientID + "'; makeMenu" + this.UniqueID + "('" + this.UniqueID + "');");
630 strInitScript.Append(" if( document.all&&window.print ) ");
631 strInitScript.Append(" { ");
632 strInitScript.Append(" var objClientId = '" + tcDown.ClientID + "';");
633 strInitScript.Append(" var obj = document.getElementById(objClientId);");
634 strInitScript.Append(" document.onclick = hideCurrentMenu;");
635 strInitScript.Append(" } ");
636 strInitScript.Append("</script>");
637
638 if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "InitScript" + this.UniqueID))
639 {
640 Page.ClientScript.RegisterStartupScript(this.GetType(), "InitScript" + this.UniqueID,
641 strInitScript.ToString());
642 }
643 base.OnPreRender(e);
644 }
645
646 #endregion
647 }
648 }
2 /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// </summary>
5
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Text;
10 using System.Web;
11 using System.Web.UI;
12 using System.Web.UI.WebControls;
13 using System.Web.UI.HtmlControls;
14 using System.IO;
15 using System.Drawing;
16
17 namespace ToolButton
18 {
19 [DefaultProperty("Text")]
20 [ToolboxData("<{0}:ToolButton runat=server></{0}:ToolButton>")]
21 public class ToolButton : CompositeControl, INamingContainer
22 {
23 #region 内部变量
24
25 //含下拉箭头单元格的ID
26 private string tcDownID = "";
27
28 //本控件的ID
29 private string tbID = "";
30
31 #endregion
32
33 #region 属性
34
35 /// <summary>
36 /// 设置或获取显示的文本
37 /// </summary>
38 [Bindable(true)]
39 [Category("Appearance")]
40 [DefaultValue("[按钮文本]")]
41 [Localizable(true)]
42 [Description("显示的文本")]
43 public string Text
44 {
45 get
46 {
47 string s = (string)ViewState["Text"];
48 return ((s == null) ? String.Empty : s);
49 }
50 set
51 {
52 ViewState["Text"] = value;
53 }
54 }
55
56 /// <summary>
57 /// 设置或获取图片路径
58 /// </summary>
59 [Bindable(true)]
60 [Category("Appearance")]
61 [DefaultValue("NoPic.gif")]
62 [Localizable(true)]
63 [Description("显示图像的文件名")]
64 public string ImageName
65 {
66 get
67 {
68 String s = (String)ViewState["ImageName"];
69 return ((s == null) ? String.Empty : s);
70 }
71
72 set
73 {
74 ViewState["ImageName"] = value;
75 }
76 }
77
78 /// <summary>
79 /// 设置或获取图片文件夹目录
80 /// </summary>
81 [Bindable(true)]
82 [Category("客户端路径")]
83 [DefaultValue("")]
84 [Localizable(true)]
85 [Description("资源(image/css/js)的客户端根目录")]
86 public string ClientPath
87 {
88 get
89 {
90 String s = (String)ViewState["ClientPath"];
91 return ((s == null) ? String.Empty : s);
92 }
93
94 set
95 {
96 ViewState["ClientPath"] = value;
97 }
98 }
99
100 /// <summary>
101 /// 设置或获取点击按钮执行的命令(点击大按钮执行的命令)
102 /// </summary>
103 [Bindable(true)]
104 [Category("行为")]
105 [DefaultValue("")]
106 [Localizable(true)]
107 [Description("点击控件执行的命令字串")]
108 public string Command
109 {
110 get
111 {
112 string s = (string)ViewState["Command"];
113 return ((s == null) ? String.Empty : s);
114 }
115 set
116 {
117 ViewState["Command"] = value;
118 }
119 }
120
121 private PopupMenu _Items = new PopupMenu();
122
123 /// <summary>
124 /// 获取子菜单集合
125 /// </summary>
126 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
127 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
128 [Description("获取或设置下拉子菜单项集合")]
129 [Category("杂项")]
130 [NotifyParentProperty(true)]
131 [TypeConverter(typeof(CollectionConverter))]
132 [DesignOnly(false)]
133 public PopupMenu Items
134 {
135 get
136 {
137 if (_Items == null)
138 {
139 _Items = new PopupMenu();
140 }
141 return _Items;
142 }
143 }
144
145 /// <summary>
146 /// 设置或获取子菜单集合句柄
147 /// </summary>
148 [Browsable(false)]
149 [Description("获取或设置菜单")]
150 public PopupMenu Menu
151 {
152 get
153 {
154 return _Items;
155 }
156 set
157 {
158 this._Items.Clear();
159 foreach (PopupMenuItem item in value)
160 {
161 this._Items.Add(item);
162 }
163 }
164 }
165
166 [Browsable(true)]
167 [Category("CSS样式")]
168 [Description("获取或设置主控件的OnMouseOver样式")]
169 public string CSS_MAIN_ONMOUSEOVER_CLASSNAME
170 {
171 get
172 {
173 string s = (string)ViewState["CSS_MAIN_ONMOUSEOVER"];
174 return ((s == null) ? String.Empty : s);
175 }
176 set
177 {
178 ViewState["CSS_MAIN_ONMOUSEOVER"] = value;
179 }
180 }
181 [Browsable(true)]
182 [Category("CSS样式")]
183 [Description("获取或设置主控件的OnMouseOut样式")]
184 public string CSS_MAIN_ONMOUSEOUT_CLASSNAME
185 {
186 get
187 {
188 string s = (string)ViewState["CSS_MAIN_ONMOUSEOUT"];
189 return ((s == null) ? String.Empty : s);
190 }
191 set
192 {
193 ViewState["CSS_MAIN_ONMOUSEOUT"] = value;
194 }
195 }
196 [Browsable(true)]
197 [Category("CSS样式")]
198 [Description("获取或设置下拉款项的OnMouseOver样式")]
199 public string CSS_SUBITEM_ONMOUSEOVER_CLASSNAME
200 {
201 get
202 {
203 string s = (string)ViewState["CSS_SUBITEM_ONMOUSEOVER"];
204 return ((s == null) ? String.Empty : s);
205 }
206 set
207 {
208 ViewState["CSS_SUBITEM_ONMOUSEOVER"] = value;
209 }
210 }
211 [Browsable(true)]
212 [Category("CSS样式")]
213 [Description("获取或设置下拉款项的OnMouseOut样式")]
214 public string CSS_SUBITEM_ONMOUSEOUT_CLASSNAME
215 {
216 get
217 {
218 string s = (string)ViewState["CSS_SUBITEM_ONMOUSEOUT"];
219 return ((s == null) ? String.Empty : s);
220 }
221 set
222 {
223 ViewState["CSS_SUBITEM_ONMOUSEOUT"] = value;
224 }
225 }
226
227
228 #endregion
229
230 #region 构造函数
231
232 /// <summary>
233 /// 构造函数
234 /// </summary>
235 public ToolButton()
236 {
237 //设置一套默认风格
238 this.BackColor = Color.FromName("MenuBar");//ViewState["BackColor"] = Color.FromName("MenuBar");
239 ViewState["Text"] = "[按钮文本]";
240 //ViewState["ClientPath"] = "ToolButtonImages";
241 ViewState["ImageName"] = "NoPic.gif";
242 this.BorderStyle = BorderStyle.NotSet;//ViewState["BorderStyle"] = BorderStyle.NotSet;
243 this.BorderWidth = Unit.Pixel(1);//ViewState["BorderWidth"] = Unit.Pixel(1);
244 this.BorderColor = Color.FromName("ControlDark");//ViewState["BorderColor"] = Color.FromName("ControlDark");
245 this.Height = Unit.Pixel(25);//ViewState["Height"] = Unit.Pixel(25);
246 //this.Width = Unit.Pixel(100);]
247 }
248
249 #endregion
250
251 #region 方法
252
253 protected override void Render(HtmlTextWriter writer)
254 {
255 //base.Render(writer);
256 PrepareControlForRendering();
257 base.RenderContents(writer);
258 }
259
260 /// <summary>
261 /// 设置控件树的样式和客户端事件
262 /// </summary>
263 private void PrepareControlForRendering()
264 {
265
266 if (this.Controls.Count < 1)
267 {
268 return;
269 }
270
271 bool IsCustomStyle = CSS_MAIN_ONMOUSEOVER_CLASSNAME != String.Empty && CSS_MAIN_ONMOUSEOUT_CLASSNAME != String.Empty;
272
273 Table t = (Table)this.Controls[0];
274 t.CellPadding = 0;
275 t.CellSpacing = 0;
276 t.Style.Add("Cursor", "default");
277 t.CopyBaseAttributes(this);
278 t.Height = this.Height;
279 t.Width = this.Width;
280 t.BorderStyle = this.BorderStyle;
281 t.BorderWidth = Unit.Pixel(0);
282 t.Attributes.Add("onselectstart", "return false;");
283
284 //t.Style.Add("aligh", "left");
285 //t.Style.Add("table-layout", "fixed");
286 //t.Style.Add("word-wrap", "break-word");
287 //***style="display:inline-block
288 //t.Style.Add("display:inline", "block");
289
290 //if (CSS_MAIN_ONMOUSEOVER_CLASSNAME != String.Empty && CSS_MAIN_ONMOUSEOUT_CLASSNAME != String.Empty)
291 //{
292 // t.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
293 // t.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "')");
294 // t.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "')");
295 //}
296
297
298 if (t.Rows.Count > 0)
299 {
300 TableRow tr = t.Rows[0];
301 //if (IsCustomStyle == false)
302 //{
303 tr.BorderWidth = Unit.Pixel(0);
304 // }
305
306
307 ////应用用户定义样式
308 //if (IsCustomStyle)
309 //{
310 // tr.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
311 // tr.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "')");
312 // tr.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "')");
313 //}
314
315 for (int i = 0; i < tr.Cells.Count; i++)
316 {
317 TableCell tc = tr.Cells[i];
318 //tc.Style.Add("align", "left");
319 if (IsCustomStyle == false)
320 {
321 tc.BorderColor = this.BorderColor;
322 tc.BorderStyle = this.BorderStyle;
323 }
324
325 string strBorderWidth = int.Parse(this.BorderWidth.Value.ToString()).ToString();
326
327 //应用用户定义样式
328 if (IsCustomStyle)
329 {
330 tc.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
331 }
332 else
333 {
334 tc.CssClass = this. CssClass;
335 }
336
337
338 if (i == 0)
339 {
340 if (IsCustomStyle == false)
341 {
342 tc.BorderWidth = this.BorderWidth;
343 tc.Style.Add("border-Width", strBorderWidth + "px 0px " + strBorderWidth + "px " + strBorderWidth + "px");
344 }
345 else
346 {
347 tc.Style.Add("border-Right-Width", "0px");
348 //固定Border-width
349 //tc.Style.Add("border-Width", "1px 0px 1px 1px");
350 }
351
352 if (this.Command != null && this.Command.Length > 0)
353 {
354 string strCommand = this.Command.Trim().Replace("\"", "'").Replace(" ", "");
355 tc.Attributes.Add("onclick", strCommand);
356
357 if (IsCustomStyle == false)
358 {
359 string str = Color.FromName("LightGray").ToString();
360 tc.Attributes.Add("onmouseenter", "this.style.borderStyle='inset'; this.parentElement.children(1).style.borderStyle='inset';");
361 tc.Attributes.Add("onmouseleave", "this.style.borderStyle='outset';this.parentElement.children(1).style.borderStyle='outset';");
362 }
363 else
364 {
365 tc.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(1), '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');");
366 tc.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(1), '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');");
367 }
368 }
369
370 //tc.Style.Add("border-color", "red");
371 //tc.Style.Add("width", ((System.Web.UI.WebControls.Image)tc.Controls[0]).Width.ToString());
372 }
373 else if (i == 1)
374 {
375 if (IsCustomStyle == false)
376 {
377 tc.BorderWidth = this.BorderWidth;
378 tc.Style.Add("border-Width", strBorderWidth + "px 0px " + strBorderWidth + "px 0px");
379 }
380 else
381 {
382 tc.Style.Add("border-Left-Width", "0px");
383 //固定Border-width
384 //tc.Style.Add("border-Width", "1px 1px 1px 0px");
385 }
386
387 if (this.Command != null && this.Command.Length > 0)
388 {
389 string strCommand = this.Command.Trim().Replace("\"", "'").Replace(" ", "");
390 tc.Attributes.Add("onclick", strCommand);
391 if (IsCustomStyle == false)
392 {
393 tc.Attributes.Add("onmouseenter", "this.style.borderStyle='inset'; this.parentElement.children(0).style.borderStyle='inset';");
394 tc.Attributes.Add("onmouseleave", "this.style.borderStyle='outset';this.parentElement.children(0).style.borderStyle='outset';");
395 // tc.CssClass = "mainexit";
396 // tc.Attributes.Add("onmouseenter", "ChangeClassName(this, 'mainfocus');ChangeClassName(this.parentElement.children(0),'mainfocus');");
397 // tc.Attributes.Add("onmouseleave", "ChangeClassName(this, 'mainexit');ChangeClassName(this.parentElement.children(0),'mainexit');");
398 }
399 else
400 {
401 tc.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(0), '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');");
402 tc.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');" + "ChangeClassName(this.parentElement.children(0), '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');");
403 }
404 }
405
406 if (IsCustomStyle == false)
407 {
408 tc.Font.MergeWith(this.Font);
409 }
410 }
411 else if (i == 2)
412 {
413 if (IsCustomStyle == false)
414 {
415 tc.BorderWidth = this.BorderWidth;
416 }
417 else
418 {
419 //固定Border-width
420 //tc.Style.Add("border-Width","1px 1px 1px 1px");
421 }
422 if (IsCustomStyle == true)
423 {
424 tc.Attributes.Add("onmouseenter", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOVER_CLASSNAME + "');");
425 tc.Attributes.Add("onmouseleave", "ChangeClassName(this, '" + CSS_MAIN_ONMOUSEOUT_CLASSNAME + "');");
426 }
427 else
428 {
429 tc.Attributes.Add("onmouseenter", "this.style.borderStyle='inset';");
430 tc.Attributes.Add("onmouseleave", "this.style.borderStyle='outset';");
431 }
432 //tc.Style.Add("border-color", "red");
433 }
434
435 if (IsCustomStyle == false)
436 {
437 tc.ForeColor = this.ForeColor;
438 tc.BackColor = this.BackColor;
439 }
440
441 if (tc.ID == tcDownID)
442 {
443 if (this.Items.Count > 0)
444 {
445 tc.Attributes.Add("onclick", "activeMenu('" + this.UniqueID + "');");
446 }
447 else
448 {
449 tc.Enabled = false;
450 //tc.Style.Add("display", "none");
451 if (tc.Parent.Controls.Count >= 2)
452 {
453 ((TableCell)tc.Parent.Controls[1]).Style.Add("border-Right-Width", "1px");
454 }
455 }
456 }
457 }
458 }
459 }
460
461 /// <summary>
462 /// 创建子控件的层次结构
463 /// </summary>
464 protected override void CreateChildControls()
465 {
466 Table t = new Table();
467 tbID = this.UniqueID + "_table";
468 t.ID = tbID;
469
470 TableRow tr = new TableRow();
471 t.Rows.Add(tr);
472
473 TableCell tc_img = new TableCell();
474 if (this.ClientPath != String.Empty || this.ImageName != String.Empty)
475 {
476 string strImageUrl = Path.Combine(this.ClientPath, "ToolButtonImages\\" + this.ImageName);
477 System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
478 image.BorderWidth = Unit.Pixel(0);
479 image.ImageUrl = strImageUrl;
480 tc_img.Controls.Add(image);
481 tr.Cells.Add(tc_img);
482 }
483
484 TableCell tc_text = new TableCell();
485 tc_text.Controls.Add(new LiteralControl(" " + this.Text + " "));
486 tr.Cells.Add(tc_text);
487
488 TableCell tc_downmenu = new TableCell();
489 tcDownID = this.UniqueID + "tcDown";
490 tc_downmenu.ID = tcDownID;
491 tc_downmenu.Controls.Add(new LiteralControl("<font size=1>▼</font>"));
492 tr.Cells.Add(tc_downmenu);
493
494 this.Controls.Add(t);
495 base.CreateChildControls();
496 }
497
498 //protected override void RenderContents(HtmlTextWriter writer)
499 //{
500 // base.RenderContents(writer);
501 //}
502
503 /// <summary>
504 /// 注册客户端脚本
505 /// </summary>
506 /// <param name="e"></param>
507 protected override void OnPreRender(EventArgs e)
508 {
509
510 //引用JS文件
511 if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "BuildMenu"))
512 {
513 //string jsPath = clientPath + "/Js/" + "jsscript.js";
514 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "BuildMenu",
515 "<script type='text/javascript' src='js/ToolButton_JScript.js'></script>");
516 }
517
518
519 ///<summary>
520 /// 注册makeMenu方法脚本
521 ///<summary>
522 StringBuilder strAddMenuItemScript = new StringBuilder();
523 if (this.Items.Count != 0)
524 {
525 //if (this.CSS_MAIN_ONMOUSEOUT_CLASSNAME != String.Empty)
526 //{
527
528 // TableCell tc = new TableCell();
529 // tc.CssClass = this.CSS_MAIN_ONMOUSEOUT_CLASSNAME;
530 // Color c = tc.BorderColor;
531 //}
532
533 strAddMenuItemScript.Append(" function makeMenu"+this.UniqueID+"(UniqueID)");
534 strAddMenuItemScript.Append(" { ");
535 strAddMenuItemScript.Append(" var myMenu, item; ");
536 strAddMenuItemScript.Append(" ");
537 strAddMenuItemScript.Append(" ");
538 strAddMenuItemScript.Append(" myMenu = new contextMenu(UniqueID); ");
539 strAddMenuItemScript.Append(" ");
540 foreach (PopupMenuItem item in this.Items)
541 {
542 string strImageSrc;
543 strImageSrc = Path.Combine(this.ClientPath, @"ToolButtonImages\" + item.ImageName);
544 strImageSrc = strImageSrc.Replace("\\", "\\\\");
545
546 string strCommand;
547 try
548 {
549 strCommand = item.Command.Trim().Replace("\"", "'").Replace(" ", "");
550 }
551 catch
552 {
553 strCommand = null;
554 }
555
556 strAddMenuItemScript.Append(" item = new contextItem('" + (item.Text == null ? " " : item.Text) + "','" + strImageSrc + "',\"" + (strCommand == null ? "" : (strCommand + "; hideCurrentMenu()")) + "\",'" + "Menu" + "','" + this.CSS_SUBITEM_ONMOUSEOVER_CLASSNAME + "','" + this.CSS_SUBITEM_ONMOUSEOUT_CLASSNAME + "');");
557 strAddMenuItemScript.Append(" myMenu.addItem(item); ");
558
559 }
560 strAddMenuItemScript.Append(" ");
561 strAddMenuItemScript.Append(" myMenu.show(this.document);");
562 strAddMenuItemScript.Append(" ");
563 strAddMenuItemScript.Append(" delete item;");
564 strAddMenuItemScript.Append(" delete myMenu;");
565 strAddMenuItemScript.Append(" }");
566 }
567 else
568 {
569 strAddMenuItemScript.Append(" function makeMenu"+this.UniqueID+"(UniqueID)");
570 strAddMenuItemScript.Append(" { ");
571 //strAddMenuItemScript.Append(" alert('No Set Items Property!');");
572 strAddMenuItemScript.Append(" }");
573 }
574
575 if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "AddMenuItemScript"+this.UniqueID))
576 {
577 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "AddMenuItemScript"+this.UniqueID, strAddMenuItemScript.ToString(), true);
578 }
579
580 /////<summary>
581 ///// 注册contextMenu方法脚本
582 /////<summary>
583 //StringBuilder strCreateDiv = new StringBuilder();
584 //strCreateDiv.Append("function contextMenu" + this.UniqueID + "()");
585 //strCreateDiv.Append("{\r\n");
586 //strCreateDiv.Append("this.items = new Array();\r\n");
587 //strCreateDiv.Append("this.addItem = function(item)\r\n");
588 //strCreateDiv.Append("{\r\n");
589 //strCreateDiv.Append(" this.items[this.items.length] = item;\r\n");
590 //strCreateDiv.Append("}\r\n");
591 //strCreateDiv.Append("this.show = function (oDoc)\r\n");
592 //strCreateDiv.Append("{\r\n");
593 //strCreateDiv.Append(" var strShow = \"\";\r\n");
594 //strCreateDiv.Append(" var i;\r\n");
595 //strCreateDiv.Append(" strShow = \"<div id='rightmenu" + this.UniqueID + "' onclick='event.cancelBubble=true;return true;' style='BACKGROUND-COLOR: system; BORDER: #000000 1px solid; LEFT: 0px; POSITION: absolute; TOP: 0px; display: none; Z-INDEX: 10'>\"; \r\n");
596 //strCreateDiv.Append(" strShow += \"<table border='0' height='\"; \r\n");
597 //strCreateDiv.Append(" strShow += this.items.length * 15;\r\n");
598 //strCreateDiv.Append(" strShow += \"' cellpadding='0' cellspacing='0'>\";\r\n");
599 //strCreateDiv.Append(" oDoc.write(strShow);\r\n");
600 //strCreateDiv.Append(" for(i=0; i<this.items.length; i++)\r\n");
601 //strCreateDiv.Append(" {\r\n");
602 //strCreateDiv.Append(" this.items[i].show(oDoc);\r\n");
603 //strCreateDiv.Append(" }\r\n");
604 //strCreateDiv.Append(" strShow += \"</table>\";\r\n");
605 //strCreateDiv.Append(" strShow += \"</div>\";\r\n");
606 //strCreateDiv.Append(" oDoc.write(strShow);\r\n");
607 //strCreateDiv.Append(" }\r\n");
608 //strCreateDiv.Append("}\r\n");
609 //if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "CreateMainDiv" + this.UniqueID))
610 //{
611 // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateMainDiv" + this.UniqueID, strCreateDiv.ToString(), true);
612 //}
613
614 ///<summary>
615 /// 注册contextMenu方法脚本
616 ///<summary>
617 // StringBuilder strChangeStyle = new StringBuilder();strChangeStyle.Append("
618
619
620
621
622 ///<summary>
623 ///添加StartUp脚本
624 ///<summary>
625 Control tcDown = this.FindControl(tcDownID);
626 Control table = this.FindControl(tbID);
627 StringBuilder strInitScript = new StringBuilder();
628 strInitScript.Append("<script text/javascript> ");
629 strInitScript.Append(" var tbClientID" + this.UniqueID + "='" + table.ClientID + "'; makeMenu" + this.UniqueID + "('" + this.UniqueID + "');");
630 strInitScript.Append(" if( document.all&&window.print ) ");
631 strInitScript.Append(" { ");
632 strInitScript.Append(" var objClientId = '" + tcDown.ClientID + "';");
633 strInitScript.Append(" var obj = document.getElementById(objClientId);");
634 strInitScript.Append(" document.onclick = hideCurrentMenu;");
635 strInitScript.Append(" } ");
636 strInitScript.Append("</script>");
637
638 if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "InitScript" + this.UniqueID))
639 {
640 Page.ClientScript.RegisterStartupScript(this.GetType(), "InitScript" + this.UniqueID,
641 strInitScript.ToString());
642 }
643 base.OnPreRender(e);
644 }
645
646 #endregion
647 }
648 }
2. 单个菜单项类PopupMenuItem代码
1 /// <summary>
2 /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// </summary>
5
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Text;
10 using System.Web.UI;
11
12 namespace ToolButton
13 {
14 /// <summary>
15 /// 下拉菜单子项类
16 /// </summary>
17 [ToolboxItem(false)]
18 public class PopupMenuItem
19 {
20 private string _ImageName;
21 private string _Text;
22 private string _Command;
23 //private ItemType _Type;
24
25 public PopupMenuItem()
26 { }
27
28 public PopupMenuItem(string _ImageName, string Text, string Command)
29 {
30 this._ImageName = _ImageName;
31 this._Text = Text;
32 this._Command = Command;
33 //this._Type = Type;
34 }
35
36 /// <summary>
37 /// 设置或获取命令图标路径
38 /// </summary>
39 public string ImageName
40 {
41 get
42 {
43 return _ImageName;
44 }
45 set
46 {
47 _ImageName = value;
48 }
49 }
50
51 /// <summary>
52 /// 设置或获取下拉子菜单项显示的文本
53 /// </summary>
54 public string Text
55 {
56 get { return _Text; }
57 set { _Text = value; }
58 }
59
60
61 /// <summary>
62 /// 设置或获取当点击下拉子菜单项时所调用的命令
63 /// </summary>
64 public string Command
65 {
66 get { return _Command; }
67 set { _Command = value; }
68 }
69
70
71 ///// <summary>
72 ///// 设置或获取子菜单项的类别(菜单项或分割符)
73 ///// </summary>
74 //public ItemType Type
75 //{
76 // get { return _Type; }
77 // set { _Type = value; }
78 //}
79
80 }
81 }
2 /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// </summary>
5
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Text;
10 using System.Web.UI;
11
12 namespace ToolButton
13 {
14 /// <summary>
15 /// 下拉菜单子项类
16 /// </summary>
17 [ToolboxItem(false)]
18 public class PopupMenuItem
19 {
20 private string _ImageName;
21 private string _Text;
22 private string _Command;
23 //private ItemType _Type;
24
25 public PopupMenuItem()
26 { }
27
28 public PopupMenuItem(string _ImageName, string Text, string Command)
29 {
30 this._ImageName = _ImageName;
31 this._Text = Text;
32 this._Command = Command;
33 //this._Type = Type;
34 }
35
36 /// <summary>
37 /// 设置或获取命令图标路径
38 /// </summary>
39 public string ImageName
40 {
41 get
42 {
43 return _ImageName;
44 }
45 set
46 {
47 _ImageName = value;
48 }
49 }
50
51 /// <summary>
52 /// 设置或获取下拉子菜单项显示的文本
53 /// </summary>
54 public string Text
55 {
56 get { return _Text; }
57 set { _Text = value; }
58 }
59
60
61 /// <summary>
62 /// 设置或获取当点击下拉子菜单项时所调用的命令
63 /// </summary>
64 public string Command
65 {
66 get { return _Command; }
67 set { _Command = value; }
68 }
69
70
71 ///// <summary>
72 ///// 设置或获取子菜单项的类别(菜单项或分割符)
73 ///// </summary>
74 //public ItemType Type
75 //{
76 // get { return _Type; }
77 // set { _Type = value; }
78 //}
79
80 }
81 }
3. 菜单集合类PopupMenu代码
1 /// <summary>
2 /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// </summary>
5
6 using System;
7 using System.Collections;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Web.UI;
11
12 namespace ToolButton
13 {
14 /// <summary>
15 /// 菜单实现类[实用泛型集合]
16 /// </summary>
17 [
18 ToolboxItem(false),
19 ParseChildren(true)
20 ]
21 public class PopupMenu : List<PopupMenuItem>
22 {
23
24 #region 定义构造函数
25
26 public PopupMenu()
27 : base()
28 {
29 }
30
31 #endregion
32
33 /// <summary>
34 /// 得到集合元素的个数
35 /// </summary>
36 public new int Count
37 {
38 get
39 {
40 return base.Count;
41 }
42 }
43
44 /// <summary>
45 /// 表示集合是否为只读
46 /// </summary>
47 public bool IsReadOnly
48 {
49 get
50 {
51 return false;
52 }
53 }
54 /// <summary>
55 /// 添加对象到集合
56 /// </summary>
57 /// <param name="item"></param>
58 public new void Add(PopupMenuItem item)
59 {
60 base.Add(item);
61 }
62
63 /// <summary>
64 /// 清空集合
65 /// </summary>
66 public new void Clear()
67 {
68 base.Clear();
69 }
70
71 /// <summary>
72 /// 判断集合中是否包含元素
73 /// </summary>
74 /// <param name="item"></param>
75 /// <returns></returns>
76 public new bool Contains(PopupMenuItem item)
77 {
78 return base.Contains(item);
79 }
80
81 /// <summary>
82 /// 移除一个对象
83 /// </summary>
84 /// <param name="item"></param>
85 /// <returns></returns>
86 public new bool Remove(PopupMenuItem item)
87 {
88 return base.Remove(item);
89 }
90
91 /// <summary>
92 /// 设置或取得下拉菜单索引项
93 /// </summary>
94 /// <param name="index"></param>
95 /// <returns></returns>
96 public new PopupMenuItem this[int index]
97 {
98 get
99 {
100 return base[index];
101 }
102 set
103 {
104 base[index] = value;
105 }
106 }
107
108 }
109 }
110
2 /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】
3 /// Blog: Http://blog.csdn.net/ChengKing
4 /// </summary>
5
6 using System;
7 using System.Collections;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Web.UI;
11
12 namespace ToolButton
13 {
14 /// <summary>
15 /// 菜单实现类[实用泛型集合]
16 /// </summary>
17 [
18 ToolboxItem(false),
19 ParseChildren(true)
20 ]
21 public class PopupMenu : List<PopupMenuItem>
22 {
23
24 #region 定义构造函数
25
26 public PopupMenu()
27 : base()
28 {
29 }
30
31 #endregion
32
33 /// <summary>
34 /// 得到集合元素的个数
35 /// </summary>
36 public new int Count
37 {
38 get
39 {
40 return base.Count;
41 }
42 }
43
44 /// <summary>
45 /// 表示集合是否为只读
46 /// </summary>
47 public bool IsReadOnly
48 {
49 get
50 {
51 return false;
52 }
53 }
54 /// <summary>
55 /// 添加对象到集合
56 /// </summary>
57 /// <param name="item"></param>
58 public new void Add(PopupMenuItem item)
59 {
60 base.Add(item);
61 }
62
63 /// <summary>
64 /// 清空集合
65 /// </summary>
66 public new void Clear()
67 {
68 base.Clear();
69 }
70
71 /// <summary>
72 /// 判断集合中是否包含元素
73 /// </summary>
74 /// <param name="item"></param>
75 /// <returns></returns>
76 public new bool Contains(PopupMenuItem item)
77 {
78 return base.Contains(item);
79 }
80
81 /// <summary>
82 /// 移除一个对象
83 /// </summary>
84 /// <param name="item"></param>
85 /// <returns></returns>
86 public new bool Remove(PopupMenuItem item)
87 {
88 return base.Remove(item);
89 }
90
91 /// <summary>
92 /// 设置或取得下拉菜单索引项
93 /// </summary>
94 /// <param name="index"></param>
95 /// <returns></returns>
96 public new PopupMenuItem this[int index]
97 {
98 get
99 {
100 return base[index];
101 }
102 set
103 {
104 base[index] = value;
105 }
106 }
107
108 }
109 }
110