自定义WebPart菜单
***********************************************************************
* 版权声明
* 此文章为ocean所有,版权归ocean所有,任何网
* 站和 媒体转载必须包含此段声明,否则将视为侵权,作
* 者将保留一切权力。此声明为此文章中不可或缺的一部分。
* 作者网名:ocean
* http://sps.oceanstudio.net
* 作者blog:博客园,http://www.cnblogs.com/ocean
* Ocean's blog,http://www.oceanstudio.net/sps/blog
* 此文章发表时间:2005年3月17日
* 作者blog:博客园,http://www.cnblogs.com/ocean
* Ocean's blog,http://www.oceanstudio.net/sps/blog
* 此文章发表时间:2005年3月17日
************************************************************************
如何能够在自定义WebPart中的菜单呢?达到下图的效果:
我还专门提供了一个示例,代码可以点击此处下载。 这个示例本身不是我写的,我只是对其进行了一下包装。
本来还作了一个演示示例, 但可惜自定义菜单后,匿名用户是无法访问的,所以只能是验证用户才可以。
同时你可以从 http://www.oceanstudio.net/sps/Lists/webpart/AllItems.aspx 查看其它的WebPart。
如何来做呢?在WebPart中我们需要重载CreateWebPartMenu()函数,这个函数是用来生成WebPart的下拉菜单的,其中在WebPart中有一个WebPartMenu对象,这个对象就是WebPart的菜单对象。通过this.WebPartMenu.MenuItems就可以得到所有的菜单项目,当然菜单是可以好多级的。生成菜单通过new MenuItem(),添加一个菜单只要通过MenuItems.Add方法即可。我们还可以为菜单添加客户端或者服务端事件。还可以标记菜单,比如那个删除菜单前面打了一个勾,这是通过MenuItem.Checked = true来实现的。废话不多说了,代码如下:
public override void CreateWebPartMenu()
{
//Add a new Menu Item with a client-side handler
MenuItem myMenuItem = new MenuItem("Client Side Handler", "javascript:alert('test');", "MyMenuItem");
this.WebPartMenu.MenuItems.Add(myMenuItem);
//Add a handler with a server-side handler
MenuItem myServerMenuItem = new MenuItem("Server Side Handler","myMenuServerID",new EventHandler(myServerMenuItem_OnClick));
this.WebPartMenu.MenuItems.Add(myServerMenuItem);
//Create a menu with children
MenuItem myParentMenuItem = new MenuItem("Parent Menu Item","javascript:alert('Parent');","myMenuParentID");
this.WebPartMenu.MenuItems.Add(myParentMenuItem);
//Add submenus to Server-side menu item
MenuItem myChildMenuItem = new MenuItem("Child menu","javascript:alert('You clicked on the child');","childmenuID");
myParentMenuItem.MenuItems.Add(myChildMenuItem);
//Check mark the Delete menu item
MenuItem deleteMenu = this.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Delete");
deleteMenu.Checked = true;
//Disable the minimize event by adding a return in Javascript
MenuItem minimizeMenu = this.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Minimize");
minimizeMenu.ClientEvent = "javascript:alert('Canceling');return false;";
//Disable all menu items
//this.WebPartMenu = null;
MenuItem item1 = new MenuItem("Menu Item 1","javascript:alert('Menu Item 1');");
MenuItem item2 = new MenuItem("Menu Item 2","javascript:alert('Menu Item 2');");
//Show using AddRange
this.WebPartMenu.MenuItems.AddRange(new MenuItem[] { item1, item2});
}
{
//Add a new Menu Item with a client-side handler
MenuItem myMenuItem = new MenuItem("Client Side Handler", "javascript:alert('test');", "MyMenuItem");
this.WebPartMenu.MenuItems.Add(myMenuItem);
//Add a handler with a server-side handler
MenuItem myServerMenuItem = new MenuItem("Server Side Handler","myMenuServerID",new EventHandler(myServerMenuItem_OnClick));
this.WebPartMenu.MenuItems.Add(myServerMenuItem);
//Create a menu with children
MenuItem myParentMenuItem = new MenuItem("Parent Menu Item","javascript:alert('Parent');","myMenuParentID");
this.WebPartMenu.MenuItems.Add(myParentMenuItem);
//Add submenus to Server-side menu item
MenuItem myChildMenuItem = new MenuItem("Child menu","javascript:alert('You clicked on the child');","childmenuID");
myParentMenuItem.MenuItems.Add(myChildMenuItem);
//Check mark the Delete menu item
MenuItem deleteMenu = this.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Delete");
deleteMenu.Checked = true;
//Disable the minimize event by adding a return in Javascript
MenuItem minimizeMenu = this.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Minimize");
minimizeMenu.ClientEvent = "javascript:alert('Canceling');return false;";
//Disable all menu items
//this.WebPartMenu = null;
MenuItem item1 = new MenuItem("Menu Item 1","javascript:alert('Menu Item 1');");
MenuItem item2 = new MenuItem("Menu Item 2","javascript:alert('Menu Item 2');");
//Show using AddRange
this.WebPartMenu.MenuItems.AddRange(new MenuItem[] { item1, item2});
}
欢迎访问海洋工作室( http://sps.oceanstudio.net )