用户定制Asp2.0 WebPart菜单
二个月前看到了VS2005下面的WebPart控件可以让用户在网页正在运行时进行用户页面定
制,觉得非常有意思,因为它里面使用了类似于portal 的技术,从而给用户以耳目一新的感觉。
由于在 Sharepoint Portal Server 中也有这样的东西,同时也感到了 sharepoint 作为一种共享类型
的服务站点给大家带来的巨大方便,所在准备在 WebPart这个组件上好好看看。但后来由于开
发任务有点紧,就放下了。好在最近1期项目做完了可以休息一下,所以开始研究一下这个东西。
因为以前看惯了sharepoint的菜单后,才发现VS2005 中的WebPart与那个sharepoint的WepPart
不太一样。特别是在WebPartZone菜单下的用户定制很不相同。有关 sharepoint下的菜单订制请
参见http://www.cnblogs.com/ocean/archive/2005/03/17/120093.html。
后来在网上没有见到太多关于VS2005 WebPart控件菜单订制的文章,有的也只是一带而过,
没办法,只能去看MSDN了,好在VS2005中的MSDN有下面的解释:
You can override the OnCreateVerbs method in a derived WebPartZoneBase zone class to add
additional WebPartVerb objects to the collection of standard verbs in your zone.
You can do this by adding verbs to the collection in the Verbs property.
同时示例代码如下:
制,觉得非常有意思,因为它里面使用了类似于portal 的技术,从而给用户以耳目一新的感觉。
由于在 Sharepoint Portal Server 中也有这样的东西,同时也感到了 sharepoint 作为一种共享类型
的服务站点给大家带来的巨大方便,所在准备在 WebPart这个组件上好好看看。但后来由于开
发任务有点紧,就放下了。好在最近1期项目做完了可以休息一下,所以开始研究一下这个东西。
因为以前看惯了sharepoint的菜单后,才发现VS2005 中的WebPart与那个sharepoint的WepPart
不太一样。特别是在WebPartZone菜单下的用户定制很不相同。有关 sharepoint下的菜单订制请
参见http://www.cnblogs.com/ocean/archive/2005/03/17/120093.html。
后来在网上没有见到太多关于VS2005 WebPart控件菜单订制的文章,有的也只是一带而过,
没办法,只能去看MSDN了,好在VS2005中的MSDN有下面的解释:
You can override the OnCreateVerbs method in a derived WebPartZoneBase zone class to add
additional WebPartVerb objects to the collection of standard verbs in your zone.
You can do this by adding verbs to the collection in the Verbs property.
同时示例代码如下:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Security.Permissions;
7using System.Collections.Generic;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13namespace Samples.AspNet.CS.Controls
14{
15/*
16This code sample creates a Web Part zone and adds the
17"Copy Web Part" verb to any control in the zone.
18*/
19[AspNetHostingPermission(SecurityAction.Demand,
20 Level = AspNetHostingPermissionLevel.Minimal)]
21[AspNetHostingPermission(SecurityAction.InheritanceDemand,
22 Level = AspNetHostingPermissionLevel.Minimal)]
23public class ZoneWithAddedVerb : WebPartZone
24{
25
26 protected override void OnCreateVerbs(WebPartVerbsEventArgs e)
27 {
28 List<WebPartVerb> newVerbs = new List<WebPartVerb>();
29 newVerbs.Add(new CopyWebPartVerb(CopyWebPartToNewOne));
30 e.Verbs = new WebPartVerbCollection(e.Verbs,newVerbs);
31 base.OnCreateVerbs(e);
32 }
33
34 void CopyWebPartToNewOne(object sender, WebPartEventArgs e)
35 {
36 WebPartManager wpmgr =
37 WebPartManager.GetCurrentWebPartManager(Page);
38 System.Web.UI.WebControls.WebParts.WebPart wp;
39 Type tp = e.WebPart.GetType();
40 wp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(tp);
41 wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
42 }
43}
44 [AspNetHostingPermission(SecurityAction.Demand,
45 Level = AspNetHostingPermissionLevel.Minimal)]
46 [AspNetHostingPermission(SecurityAction.InheritanceDemand,
47 Level = AspNetHostingPermissionLevel.Minimal)]
48 internal class CopyWebPartVerb : WebPartVerb
49 {
50 private const String _copyWebPartImageUrl = "~/CopyVerb.ico";
51
52 internal CopyWebPartVerb(WebPartEventHandler serverClickHandler) :
53 base("MyVerb", serverClickHandler)
54 { }
55 public override string Text
56 {
57 get { return "Copy Web Part"; }
58 set { ;}
59 }
60 public override string Description
61 {
62 get { return "This verb will copy this web part control " +
63 "to a new one below"; }
64 set { ; }
65 }
66 public override bool Enabled
67 {
68 get { return base.Enabled; }
69 set { base.Enabled = value; }
70 }
71
72 public override string ImageUrl
73 {
74 get { return _copyWebPartImageUrl; }
75 set { ; }
76 }
77 }
78}
79
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Security.Permissions;
7using System.Collections.Generic;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13namespace Samples.AspNet.CS.Controls
14{
15/*
16This code sample creates a Web Part zone and adds the
17"Copy Web Part" verb to any control in the zone.
18*/
19[AspNetHostingPermission(SecurityAction.Demand,
20 Level = AspNetHostingPermissionLevel.Minimal)]
21[AspNetHostingPermission(SecurityAction.InheritanceDemand,
22 Level = AspNetHostingPermissionLevel.Minimal)]
23public class ZoneWithAddedVerb : WebPartZone
24{
25
26 protected override void OnCreateVerbs(WebPartVerbsEventArgs e)
27 {
28 List<WebPartVerb> newVerbs = new List<WebPartVerb>();
29 newVerbs.Add(new CopyWebPartVerb(CopyWebPartToNewOne));
30 e.Verbs = new WebPartVerbCollection(e.Verbs,newVerbs);
31 base.OnCreateVerbs(e);
32 }
33
34 void CopyWebPartToNewOne(object sender, WebPartEventArgs e)
35 {
36 WebPartManager wpmgr =
37 WebPartManager.GetCurrentWebPartManager(Page);
38 System.Web.UI.WebControls.WebParts.WebPart wp;
39 Type tp = e.WebPart.GetType();
40 wp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(tp);
41 wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
42 }
43}
44 [AspNetHostingPermission(SecurityAction.Demand,
45 Level = AspNetHostingPermissionLevel.Minimal)]
46 [AspNetHostingPermission(SecurityAction.InheritanceDemand,
47 Level = AspNetHostingPermissionLevel.Minimal)]
48 internal class CopyWebPartVerb : WebPartVerb
49 {
50 private const String _copyWebPartImageUrl = "~/CopyVerb.ico";
51
52 internal CopyWebPartVerb(WebPartEventHandler serverClickHandler) :
53 base("MyVerb", serverClickHandler)
54 { }
55 public override string Text
56 {
57 get { return "Copy Web Part"; }
58 set { ;}
59 }
60 public override string Description
61 {
62 get { return "This verb will copy this web part control " +
63 "to a new one below"; }
64 set { ; }
65 }
66 public override bool Enabled
67 {
68 get { return base.Enabled; }
69 set { base.Enabled = value; }
70 }
71
72 public override string ImageUrl
73 {
74 get { return _copyWebPartImageUrl; }
75 set { ; }
76 }
77 }
78}
79
我把上面的代码捎作修改为一个控件编译后再加载到一个WEB项目中,结果真的出现了一个MyVerb
菜单项。效果如图1:
代码如下
:My_Calender.cs
1using System;
2using System.ComponentModel;
3using System.Web;
4using System.Web.Security;
5using System.Security.Permissions;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11namespace DotnetPortal.MyWebPart
12{
13
14 [ToolboxData("<{0}:MyCalender runat=server></{0}:MyCalender>")]
15 class MyCalender : WebPart
16 {
17 protected System.Web.UI.WebControls.Calendar mc = new Calendar();
18
19 public MyCalender()
20 {
21 this.Title = "当前日期";
22 this.ExportMode = WebPartExportMode.All;
23
24 mc.SelectedDate = DateTime.Now;
25 this.Controls.Add(mc);
26 }
27
28 Public Properties
31
32 /// <summary>
33 /// Render this Web Part to the output parameter specified.
34 /// </summary>
35 /// <param name="output"> The HTML writer to write out to </param>
36 protected override void RenderContents(HtmlTextWriter output)
37 {
38
39 CreateChildControls();
40 base.RenderContents(output);
41 }
42
43
44 }
45
46 [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
47 [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 class My_Calender : WebPartVerb
49 {
50 private const string _WebPartImageUrl = "../images/xp/ALLMEET.GIF";
51
52 internal My_Calender(WebPartEventHandler serverClickHandler)
53 : base("My_Calender", serverClickHandler)
54 {
55 }
56
57 public void AlarmRang(Object sender, WebPartEventArgs e)
58 {
59 }
60
61 public override string Text
62 {
63 get { return " 我的日历"; }
64 }
65
66 public override string Description
67 {
68 get
69 {
70 return "This verb will copy this web part control to a new one below";
71 }
72 }
73 public override bool Enabled
74 {
75 get { return base.Enabled; }
76 set { base.Enabled = value; }
77 }
78
79 public override string ImageUrl
80 {
81 get { return _WebPartImageUrl; }
82 }
83 }
84}
2using System.ComponentModel;
3using System.Web;
4using System.Web.Security;
5using System.Security.Permissions;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11namespace DotnetPortal.MyWebPart
12{
13
14 [ToolboxData("<{0}:MyCalender runat=server></{0}:MyCalender>")]
15 class MyCalender : WebPart
16 {
17 protected System.Web.UI.WebControls.Calendar mc = new Calendar();
18
19 public MyCalender()
20 {
21 this.Title = "当前日期";
22 this.ExportMode = WebPartExportMode.All;
23
24 mc.SelectedDate = DateTime.Now;
25 this.Controls.Add(mc);
26 }
27
28 Public Properties
31
32 /// <summary>
33 /// Render this Web Part to the output parameter specified.
34 /// </summary>
35 /// <param name="output"> The HTML writer to write out to </param>
36 protected override void RenderContents(HtmlTextWriter output)
37 {
38
39 CreateChildControls();
40 base.RenderContents(output);
41 }
42
43
44 }
45
46 [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
47 [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 class My_Calender : WebPartVerb
49 {
50 private const string _WebPartImageUrl = "../images/xp/ALLMEET.GIF";
51
52 internal My_Calender(WebPartEventHandler serverClickHandler)
53 : base("My_Calender", serverClickHandler)
54 {
55 }
56
57 public void AlarmRang(Object sender, WebPartEventArgs e)
58 {
59 }
60
61 public override string Text
62 {
63 get { return " 我的日历"; }
64 }
65
66 public override string Description
67 {
68 get
69 {
70 return "This verb will copy this web part control to a new one below";
71 }
72 }
73 public override bool Enabled
74 {
75 get { return base.Enabled; }
76 set { base.Enabled = value; }
77 }
78
79 public override string ImageUrl
80 {
81 get { return _WebPartImageUrl; }
82 }
83 }
84}
文件:My_WebPartZone.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Security.Permissions;
7using System.Collections.Generic;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13namespace DotnetPortal.MyWebPart
14{
15 [AspNetHostingPermission(SecurityAction.Demand,
16 Level = AspNetHostingPermissionLevel.Minimal)]
17 [AspNetHostingPermission(SecurityAction.InheritanceDemand,
18 Level = AspNetHostingPermissionLevel.Minimal)]
19 public class MyWebPartZone : WebPartZone
20 {
21 public MyWebPartZone()
22 {
23 base.VerbButtonType = ButtonType.Button;
24 //base.CloseVerb.Enabled = true;
25 }
26
27 protected override void OnCreateVerbs(WebPartVerbsEventArgs e)
28 {
29 List<WebPartVerb> newVerbs = new List<WebPartVerb>();
30 newVerbs.Add(new WeatherReport(ShowMyWeather));
31 newVerbs.Add(new My_Calender(ShowMyCalender));
32 newVerbs.Add(new RSSReader(ShowMyRSS));
33 newVerbs.Add(new UpLoadFile(UpLoadMyFile));
34 e.Verbs = new WebPartVerbCollection(e.Verbs, newVerbs);
35 base.OnCreateVerbs(e);
36 }
37
38
39 void ShowMyWeather(object sender, WebPartEventArgs e)
40 {
41 WebPartManager wpmgr = WebPartManager.GetCurrentWebPartManager(Page);
42 wpmgr.AddWebPart(new MyWeatherReport(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
43 }
44
45 void ShowMyCalender(object sender, WebPartEventArgs e)
46 {
47 WebPartManager wpmgr = WebPartManager.GetCurrentWebPartManager(Page);
48 wpmgr.AddWebPart(new MyCalender(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
49 }
50
51 void ShowMyRSS(object sender, WebPartEventArgs e)
52 {
53 WebPartManager wpmgr = WebPartManager.GetCurrentWebPartManager(Page);
54 wpmgr.AddWebPart(new MyRSSReader(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
55 }
56
57 void UpLoadMyFile(object sender, WebPartEventArgs e)
58 {
59
60 WebPartManager wpmgr =WebPartManager.GetCurrentWebPartManager(Page);
61 /*
62 System.Web.UI.WebControls.WebParts.WebPart wp;
63 Type tp = e.WebPart.GetType();
64 wp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(tp);
65 wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
66 */
67 wpmgr.AddWebPart(new UpLoad(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
68
69 //Page.RegisterStartupScript("", "<script language=javascript>alert('操作已成功提交,请单击关闭按钮并返回!');</script>");
70
71 }
72
73
74
75 }
76}
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Security.Permissions;
7using System.Collections.Generic;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13namespace DotnetPortal.MyWebPart
14{
15 [AspNetHostingPermission(SecurityAction.Demand,
16 Level = AspNetHostingPermissionLevel.Minimal)]
17 [AspNetHostingPermission(SecurityAction.InheritanceDemand,
18 Level = AspNetHostingPermissionLevel.Minimal)]
19 public class MyWebPartZone : WebPartZone
20 {
21 public MyWebPartZone()
22 {
23 base.VerbButtonType = ButtonType.Button;
24 //base.CloseVerb.Enabled = true;
25 }
26
27 protected override void OnCreateVerbs(WebPartVerbsEventArgs e)
28 {
29 List<WebPartVerb> newVerbs = new List<WebPartVerb>();
30 newVerbs.Add(new WeatherReport(ShowMyWeather));
31 newVerbs.Add(new My_Calender(ShowMyCalender));
32 newVerbs.Add(new RSSReader(ShowMyRSS));
33 newVerbs.Add(new UpLoadFile(UpLoadMyFile));
34 e.Verbs = new WebPartVerbCollection(e.Verbs, newVerbs);
35 base.OnCreateVerbs(e);
36 }
37
38
39 void ShowMyWeather(object sender, WebPartEventArgs e)
40 {
41 WebPartManager wpmgr = WebPartManager.GetCurrentWebPartManager(Page);
42 wpmgr.AddWebPart(new MyWeatherReport(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
43 }
44
45 void ShowMyCalender(object sender, WebPartEventArgs e)
46 {
47 WebPartManager wpmgr = WebPartManager.GetCurrentWebPartManager(Page);
48 wpmgr.AddWebPart(new MyCalender(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
49 }
50
51 void ShowMyRSS(object sender, WebPartEventArgs e)
52 {
53 WebPartManager wpmgr = WebPartManager.GetCurrentWebPartManager(Page);
54 wpmgr.AddWebPart(new MyRSSReader(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
55 }
56
57 void UpLoadMyFile(object sender, WebPartEventArgs e)
58 {
59
60 WebPartManager wpmgr =WebPartManager.GetCurrentWebPartManager(Page);
61 /*
62 System.Web.UI.WebControls.WebParts.WebPart wp;
63 Type tp = e.WebPart.GetType();
64 wp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(tp);
65 wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
66 */
67 wpmgr.AddWebPart(new UpLoad(), e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
68
69 //Page.RegisterStartupScript("", "<script language=javascript>alert('操作已成功提交,请单击关闭按钮并返回!');</script>");
70
71 }
72
73
74
75 }
76}
然后一不做二不休,把以前经常做的几个自定义控件和一个RSS控件都转为这种WebPart控件。
但这些控件有一些区别,特别是在重写方法时,因为WebPart 要重写RenderContents方法,以提
交内容,而自定义控件是要重写Render(HtmlTextWriter output)方法,另外在属性上要注意WebPart
支持[Personalizable]和[WebBrowsable],以便进行用户自定义数据的保存。最后终于把几个控
件和功能放入到了菜单项上,如图2:
由于还有功能需要完善,所以这次暂不把源码发布出来,希望能尽快完成给大家使用。
相关链接参见:ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref16/html/
M_System_Web_UI_WebControls_WebParts_WebPartZoneBase_OnCreateVerbs_1_2240162f.htm