56gt 发表于 2015-9-29 05:20:56

Sharepoint自定义菜单栏

  Sharepoint2010的UI有很大的改观,一个重要的体现是在Ribbon。然而有些人并不喜欢这种操作,所以我想到之前的2007的工具栏。
  在工具栏里面添加了新建,和删除项目,及切换视图。
  效果如下图:

  1:ToolBar
  需要先放置新建,删除项目按钮的容器。在Sharepoint里面有提供一个用户自定义控件,位置存放在C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES/ToolBar.ascx。因为想偷懒,这个控件都已经做好了样式,直接拿过来用就好了,当然也可以自己去修改样式。该ToolBar.ascx的代码如下:
<%@ Control Language=&quot;C#&quot; Inherits=&quot;Microsoft.SharePoint.WebControls.ToolBar,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c&quot;   AutoEventWireup=&quot;false&quot; compilationMode=&quot;Always&quot; %>
<%@ Register Tagprefix=&quot;wssawc&quot; Namespace=&quot;Microsoft.SharePoint.WebControls&quot; Assembly=&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %> <%@ Register Tagprefix=&quot;SharePoint&quot; Namespace=&quot;Microsoft.SharePoint.WebControls&quot; Assembly=&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %>
<table class=&quot;<%=CssClass%>&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; id=&quot;<%=ClientID%>&quot; width=&quot;100%&quot;>
<tr>
<%-- Buttons on the left --%>
<wssawc:RepeatedControls id=&quot;RptControls&quot; runat=&quot;server&quot;>
<HeaderHtml/>
<BeforeControlHtml>
<td class=&quot;ms-toolbar&quot; nowrap=&quot;nowrap&quot;>
</BeforeControlHtml>
<AfterControlHtml>
</td>
</AfterControlHtml>
<SeparatorHtml>
<td class=&quot;ms-separator&quot;>|</td>
</SeparatorHtml>
<FooterHtml/>
</wssawc:RepeatedControls>
<td width=&quot;99%&quot; class=&quot;ms-toolbar&quot; nowrap=&quot;nowrap&quot;><img src=&quot;/_layouts/images/blank.gif&quot; width='1' height='18' alt=&quot;&quot; /></td>
<%-- Buttons on the right --%>
<wssawc:RepeatedControls id=&quot;RightRptControls&quot; runat=&quot;server&quot;>
<HeaderHtml/>
<BeforeControlHtml>
<td class=&quot;ms-toolbar&quot; nowrap=&quot;nowrap&quot;>
</BeforeControlHtml>
<AfterControlHtml>
</td>
</AfterControlHtml>
<SeparatorHtml>
<td class=&quot;ms-separator&quot;>|</td>
</SeparatorHtml>
<FooterHtml/>
</wssawc:RepeatedControls>
</tr>
</table>
  如果需要修改样式,建议在自己的样式表里面,重写这些定义的样式为好。
  我们引用这个工具栏控件:

<%@ Register Src=&quot;/_controltemplates/ToolBar.ascx&quot; TagPrefix=&quot;YLSoft&quot; TagName=&quot;ToolBar&quot; %>
  

<YLSoft:ToolBar ID=&quot;tbar&quot; runat=&quot;server&quot;>
<Template_Buttons>
</Template_Buttons>
<Template_RightButtons>
</Template_RightButtons>
</YLSoft:ToolBar>
  
  在Template_Buttons和Template_RightButtons分别放置左边,和最右边的按钮集合,一般我们视图选择都放在右边。里面直接拖拽控件也可以,为了美观,这里还是用到Sharepoint的几个控件 。
  Menu:

<SharePoint:Menu ID=&quot;MenuNew&quot; runat=&quot;server&quot; TemplateId=&quot;MenuTemplateNew&quot; Text=&quot;<%$Resources:wss,multipages_new_menu_text%>&quot;
MenuFormat=&quot;ArrowAlwaysVisible&quot; MenuAlignment=&quot;Left&quot; HoverCellActiveCssClass=&quot;ms-menubuttonactivehover&quot;
HoverCellInActiveCssClass=&quot;ms-menubuttoninactivehover&quot; AccessKey=&quot;<%$Resources:wss,tb_NewMenu_AK%>&quot; />
  这里有使用到资源文件,<%$Resources:wss,multipages_new_menu_text%>,意思是调用的是wss.resx,key为multipages_new_menu_text的资源。资源文件放在:目录下面。multipages_new_menu_text的值是New(新建)。

<data name=&quot;multipages_new_menu_text&quot;>
<value>New</value>
</data>
  那这个MenuNew西面有哪些子菜单呢?这里有一个很重要的属性:TemplateId。TemplateId指明从哪个Template获取子菜单。这里我指明的是MenuTemplateNew。看看它的定义:

<SharePoint:FeatureMenuTemplate ID=&quot;MenuTemplateNew&quot; LargeIconMode=&quot;TRUE&quot; runat=&quot;server&quot; GroupId=&quot;NewMenu&quot;>
<SharePoint:NewMenu MenuAlignment=&quot;Default&quot; ID=&quot;newMenu&quot; runat=&quot;server&quot;>
</SharePoint:NewMenu>
</SharePoint:FeatureMenuTemplate>
  我在MenuTemplateNew的菜单模板下面放置了新增菜单。看看效果

  NewMenu都是取得当前列表的新增的内容类型。当然也可以自己编码来添加新增的内容。这貌似需要继承来实现。之前有重写过这个NewMenu。
  删除项目菜单:
  <YLSoft:ToolBarButton ID=&quot;btnDel&quot; OnClientClick=&quot;getSels();return false;&quot; runat=&quot;server&quot; Text=&quot;<%$Resources:wss,setanon_deleteitems_display %>&quot;></YLSoft:ToolBarButton>
  ToolBarButton样式都Sharepoint的,所以看上去顺眼点,如果直接放Button有点恶心。
  最后UI代码如下:

<%@ Assembly Name=&quot;$SharePoint.Project.AssemblyFullName$&quot; %>
<%@ Assembly Name=&quot;Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %>
<%@ Register TagPrefix=&quot;SharePoint&quot; Namespace=&quot;Microsoft.SharePoint.WebControls&quot;
Assembly=&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %>
<%@ Register TagPrefix=&quot;Utilities&quot; Namespace=&quot;Microsoft.SharePoint.Utilities&quot; Assembly=&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %>
<%@ Register TagPrefix=&quot;asp&quot; Namespace=&quot;System.Web.UI&quot; Assembly=&quot;System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot; %>
<%@ Import Namespace=&quot;Microsoft.SharePoint&quot; %>
<%@ Register TagPrefix=&quot;WebPartPages&quot; Namespace=&quot;Microsoft.SharePoint.WebPartPages&quot;
Assembly=&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %>
<%@ Control Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;ListToolBarUserControl.ascx.cs&quot;
Inherits=&quot;YLSoft.Features.Webparts.ListToolBar.ListToolBarUserControl&quot; %>
<%@ Register Src=&quot;/_controltemplates/ToolBar.ascx&quot; TagPrefix=&quot;YLSoft&quot; TagName=&quot;ToolBar&quot; %>
<%@ Register TagPrefix=&quot;YLSoft&quot; TagName=&quot;ToolBarButton&quot; src=&quot;~/_controltemplates/ToolBarButton.ascx&quot; %>
<!-- Templates -->
<SharePoint:FeatureMenuTemplate ID=&quot;MenuTemplateNew&quot; LargeIconMode=&quot;TRUE&quot; runat=&quot;server&quot; GroupId=&quot;NewMenu&quot;>
<SharePoint:NewMenu MenuAlignment=&quot;Default&quot; ID=&quot;newMenu&quot; runat=&quot;server&quot;>
</SharePoint:NewMenu>
</SharePoint:FeatureMenuTemplate>
<SharePoint:MenuTemplate runat=&quot;server&quot; ID=&quot;MenuTemplateView&quot;>
<SharePoint:ViewSelectorMenu ID=&quot;viewSelector&quot; runat=&quot;server&quot;>
</SharePoint:ViewSelectorMenu>
</SharePoint:MenuTemplate>
<!--End Template-->
<YLSoft:ToolBar ID=&quot;tbar&quot; runat=&quot;server&quot;>
<Template_Buttons>
<SharePoint:Menu ID=&quot;MenuNew&quot; runat=&quot;server&quot; TemplateId=&quot;MenuTemplateNew&quot; Text=&quot;<%$Resources:wss,multipages_new_menu_text%>&quot;
MenuFormat=&quot;ArrowAlwaysVisible&quot; MenuAlignment=&quot;Left&quot; HoverCellActiveCssClass=&quot;ms-menubuttonactivehover&quot;
HoverCellInActiveCssClass=&quot;ms-menubuttoninactivehover&quot; AccessKey=&quot;<%$Resources:wss,tb_NewMenu_AK%>&quot; />
<YLSoft:ToolBarButton ID=&quot;btnDel&quot; OnClientClick=&quot;getSels();return false;&quot; runat=&quot;server&quot; Text=&quot;<%$Resources:wss,setanon_deleteitems_display %>&quot;></YLSoft:ToolBarButton>
</Template_Buttons>
<Template_RightButtons>
<asp:PlaceHolder ID=&quot;PlaceHolder1&quot; runat=&quot;server&quot;>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style='margin-right: 4px'>
<tr>
<td nowrap=&quot;nowrap&quot; class=&quot;ms-toolbar&quot; id=&quot;topPagingCell&quot;></td>
<td></td>
<td nowrap=&quot;nowrap&quot;> </td>
<td nowrap=&quot;nowrap&quot; class=&quot;ms-listheaderlabel&quot;><SharePoint:EncodedLiteral ID=&quot;EncodedLiteral1&quot; runat=&quot;server&quot; text=&quot;<%$Resources:wss,view_selector_view%>&quot; EncodeMethod='HtmlEncode'/> </td>
<td nowrap=&quot;nowrap&quot; class=&quot;ms-viewselector&quot; onmouseover=&quot;this.className='ms-viewselectorhover'&quot; onmouseout=&quot;this.className='ms-viewselector'&quot; id=&quot;onetPeopleViewSelector&quot;>
<SharePoint:Menu ID=&quot;MenuViewSelector&quot; runat=&quot;server&quot; TemplateId=&quot;MenuTemplateView&quot;
Text=&quot;<%$Resources:wss,mngsubwebs_selectview%>&quot; MenuFormat=&quot;ArrowAlwaysVisible&quot;
MenuAlignment=&quot;Right&quot; AlignmentElementOverrideClientId=&quot;onetPeopleViewSelector&quot;
HoverCellActiveCssClass=&quot;ms-viewselectorhover&quot; HoverCellInActiveCssClass=&quot;ms-viewselector&quot;
AccessKey=&quot;<%$Resources:wss,tb_ViewSelector_AK%>&quot; />
</td>
</tr>
</table>
</asp:PlaceHolder>
</Template_RightButtons>
</YLSoft:ToolBar>
页: [1]
查看完整版本: Sharepoint自定义菜单栏