转:http://vickynuli.blog.163.com/blog/static/180438492201281434249486/
在网上找到篇文章,自定义快速启动栏和顶部链接栏,以下为代码,根据文章的思路,需要两个方法(个人觉得这两个方法很蠢),首先在页面上放两个treeview 和四个文本框,两个按钮(一个是对顶部链接栏有效,另一个是对快速启动栏有效),
首先咱们在load事件里面把快速启动栏和顶部链接栏分别在TreeView下面显示。
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Navigation;
using System.Web.UI.WebControls;
namespace spInfo.Layouts
{
public partial class secondaryDirectory : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SPWeb web = SPContext.Current.Web;
SPNavigationNodeCollection topNav = web.Navigation.TopNavigationBar;
SPNavigationNodeCollection qucikNav = web.Navigation.QuickLaunch;
topNavTreeView.Nodes.Add(new TreeNode("Top Link Bar Links"));
quickLaunchTreeView.Nodes.Add(new TreeNode("Quick Launch Links"));
RecurseNodes(topNav, topNavTreeView.Nodes[0].ChildNodes);
RecurseNodes(qucikNav, quickLaunchTreeView.Nodes[0].ChildNodes);
topNavTreeView.ExpandAll();
quickLaunchTreeView.ExpandAll();
}
}
/// <summary>
/// 把主导航值传进来,树视图显示
/// </summary>
/// <param name="nodes">导航</param>
/// <param name="treeNodes">树</param>
private void RecurseNodes(SPNavigationNodeCollection nodes, TreeNodeCollection treeNodes)
{
foreach (SPNavigationNode node in nodes)
{
TreeNode treeNode = new TreeNode();
treeNode.Text = node.Title;
treeNode.ToolTip = node.Url;
treeNode.ShowCheckBox = true;
treeNode.Checked = node.IsExternal;
treeNode.Value = node.Id.ToString();
treeNodes.Add(treeNode);
RecurseNodes(node.Children, treeNode.ChildNodes);
}
}
/// <summary>
/// 顶部链接栏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void addTopButton_Click(object sender, EventArgs e)
{
//string vaaa = "";
//foreach (TreeNode n in topNavTreeView.CheckedNodes)
//{
// vaaa += n.Value + ",";
//}
SPWeb web = SPContext.Current.Web;
SPNavigationNode node = new SPNavigationNode(topNavTitleTextBox.Text, topNavUrlTextBox.Text, topNavIsExternalCheckBox.Checked);
if (topNavTreeView.SelectedNode == topNavTreeView.Nodes[0])
{
web.Navigation.TopNavigationBar.AddAsLast(node);
}
else
{
int parentId = 0;
foreach (TreeNode n in topNavTreeView.CheckedNodes)
{
parentId += Convert.ToInt32(n.Value);
}
//parentId = int.Parse(topNavTreeView.SelectedNode.Value);
web.Navigation.GetNodeById(parentId).Children.AddAsLast(node);
}
if (topNavOpenInNewWindowCheckBox.Checked)
{
node.Properties["Target"] = "_blank";
node.Update();
}
topNavTreeView.ExpandAll();
quickLaunchTreeView.ExpandAll();
}
/// <summary>
/// 快速启动链接栏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnQuick_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPNavigationNode node = new SPNavigationNode(tbQTitle.Text, tbQUrl.Text, CheckBox3.Checked);
if (quickLaunchTreeView.SelectedNode == quickLaunchTreeView.Nodes[0])
{
web.Navigation.QuickLaunch.AddAsLast(node);
}
else
{
int parentId = 0;
foreach (TreeNode n in quickLaunchTreeView.CheckedNodes)
{
parentId += Convert.ToInt32(n.Value);
}
//parentId = int.Parse(topNavTreeView.SelectedNode.Value);
web.Navigation.GetNodeById(parentId).Children.AddAsLast(node);
}
if (CheckBox4.Checked)
{
node.Properties["Target"] = "_blank";
node.Update();
}
topNavTreeView.ExpandAll();
quickLaunchTreeView.ExpandAll();
}
}
}
HTML代码(比较难看)
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div id="mdA">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td rowspan="6">
<asp:TreeView ID="topNavTreeView" ons runat="server">
</asp:TreeView>
</td>
<td>
Title:<asp:TextBox ID="topNavTitleTextBox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
Url:<asp:TextBox ID="topNavUrlTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="topNavIsExternalCheckBox" runat="server" Text="链接"
/>
</td>
</tr>
<tr>
<td><asp:CheckBox ID="topNavOpenInNewWindowCheckBox" runat="server" Text="在新窗口打开"
/>
</td>
</tr>
<tr>
<td>
<asp:Button ID="addTopButton" runat="server" onclick="addTopButton_Click" Text="添加到节点" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td rowspan="6">
<asp:TreeView ID="quickLaunchTreeView" runat="server">
</asp:TreeView>
</td>
<td> Title:<asp:TextBox ID="tbQTitle" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> Url:<asp:TextBox ID="tbQUrl" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td><asp:CheckBox ID="CheckBox3" runat="server" Text="链接"
/>
</td>
</tr>
<tr>
<td><asp:CheckBox ID="CheckBox4" runat="server" Text="在新窗口打开"
/>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnQuick" runat="server" onclick="btnQuick_Click" Text="添加到快速启动栏" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
</asp:Content>