65.9K
CodeProject 正在变化。 阅读更多。
Home

自定义快速启动 Web 部件

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2009年12月17日

CPOL

1分钟阅读

viewsIcon

32846

downloadIcon

303

Sharepoint 快速启动 Web 部件

CustomizeQuickLaunch

引言

SharePoint 2007 是一个优秀的 Web 门户解决方案,几乎完美地吸引了企业的注意。SharePoint 对象模型赋予了开发者根据自身需求自定义 SharePoint 的强大能力。这里自定义 SharePoint 快速启动,就像您在上面看到的。

背景

SharePoint 中的快速启动用于用户快速导航到列表、文档库、站点等。问题在于,添加的站点和列表越多,快速启动就越大,为了自定义快速启动使其可折叠,我们在这里使用 SharePoint 对象模型来构建快速启动 Web 部件。

Using the Code

使用 SharePoint Visual Studio 模板创建一个 Web 部件

初始化树视图图像集属性,通过更改 Web 部件属性来更改图像集。此属性将在属性窗格中显示为下拉列表。

// A treeview control image set enum variable
protected TreeViewImageSet _qlFormat;  
/// property use to change the image set of tree view control
[Personalizable(PersonalizationScope.Shared), 
WebBrowsable, WebDisplayName("Format for quick launch"),
WebDescription("")]
[DefaultValue(TreeViewImageSet.Arrows)]
[WebPartStorage(Storage.Personal)]
[FriendlyName("Format List")]
[Description("Select a value from the dropdown list.")]
[Browsable(true)]
public TreeViewImageSet SelectFormate
{
    set { _qlFormat = value; }
    get { return _qlFormat; }
} 

将控件添加到 Web 部件

TreeView trViewQL; // tree view control variable
protected override void CreateChildControls()
{
    base.CreateChildControls();

    trViewQL = new TreeView();
    // add control to webpart
    this.Controls.Add(trViewQL);
}  

我们需要维护 treeview 的状态。因此,我们将 querystring 附加到每个节点的 URL。

TreeNode trChild = new TreeNode();
trChild.Text = child.Title;
// set the url for maintaining the state of the control
string separate = "";
if (child.Url.IndexOf('?') == -1) // checking if URL has already 
                                  // some query string variable attached
{
     separate = "?CNID=";
}
else
{
     separate = "&CNID=";
}
trChild.NavigateUrl = child.Url +separate+child.Title; 

该方法用于在 treeview 控件中构建快速启动。我将它们放在 OnPreRender 方法中吗?答案是,通过将它们放在这里,每次 Web 部件属性更改时,都会反映出更改。

protected override void OnPreRender(EventArgs e)
    {
	// for maintaining the state of the control. 
         string currentId = "";
         if (System.Web.HttpContext.Current.Request.QueryString.Get("CNID") != null)
         {
             currentId = 
	     System.Web.HttpContext.Current.Request.QueryString.Get("CNID").ToString();
         }
         trViewQL.Nodes.Clear();
	/// apply the image set in the property
         trViewQL.ImageSet = SelectFormate;
         base.OnPreRender(e);
         trViewQL.CollapseAll();

         SPWeb web = SPContext.Current.Web;
	        
	         SPNavigationNodeCollection qlNodes = web.Navigation.QuickLaunch;
             foreach (SPNavigationNode node in qlNodes)
	         {
	             if (node.IsExternal)
                  {
                     bool IsparentOpen = false;
                     TreeNode trParent = new TreeNode();
                     trParent.Text = node.Title;
                     trParent.NavigateUrl = node.Url;

                     SPNavigationNodeCollection children = node.Children;
	                   foreach (SPNavigationNode child in children)
                     {
                         TreeNode trChild = new TreeNode();
                         trChild.Text = child.Title;
	                // set the URL for maintaining the state of the control
                         string separate = "";
                         if (child.Url.IndexOf('?') == -1)
                         {
                             separate = "?CNID=";
                         }
                         else
                         {
                             separate = "&CNID=";
                         }
                         trChild.NavigateUrl = child.Url +separate+child.Title;
	
                         if (!IsparentOpen)
                         {
			  // expand the clicked node and collapse others
                             if (currentId.Equals(child.Title))
                             {
                                 trParent.Expanded = true;
                                 trChild.Selected = true;
                                 IsparentOpen = true;
                             }
                             else
                             {
                                 trParent.Expanded = false;
                             }
                          }
                          trParent.ChildNodes.Add(trChild);
	                 }
                     trViewQL.Nodes.Add(trParent);
	            }
	         }
     }

最后,通过重写 Render 方法来渲染控件

protected override void Render(HtmlTextWriter writer)
{
     trViewQL.RenderControl(writer);
}

构建并部署项目

使用 Visual Studio 构建并部署您的项目。

历史

  • 2009年12月17日:初始发布
© . All rights reserved.