微软技术交流QQ群:69022156欢迎你的到来。
页面代码:
<table class="menu_list" id="tab_topmenu" cellpadding="5">
<tr>
<asp:Repeater ID="repNav" runat="server" OnItemDataBound="repNav_ItemDataBound">
<ItemTemplate>
<td>
<%# Container.ItemIndex==7 ?"</td><tr><td>":"" %>
<table cellpadding="0" cellspacing="0" id="tabover_<%# Container.ItemIndex+1 %>">
<tr id="topmenu_<%# Container.ItemIndex+1 %>">
<td>
<table cellpadding="0" cellspacing="0" style="vertical-align:middle;">
<tr>
<td class="menutop_left_hover" id="menutop_left<%# Container.ItemIndex+1 %>" >
</td>
<td id="menutop_center<%# Container.ItemIndex+1 %>">
<span οnclick="changeLocation('<%#Eval("NavUrl") %>')"><%#Eval("NavName") %></span>
<%# (bool)Eval("IsHaveChild")==true?"<img src='/_layouts/Infinite/images/images/dropdown.png'/>":"" %>
</td>
<td class="menutop_right_hover" id="menutop_right<%# Container.ItemIndex+1 %>" >
</td>
<td style=" padding-left:10px;"><%# Container.ItemIndex ==0 ? "|" : ""%><%# Container.ItemIndex == 0 || Container.ItemIndex == Convert.ToInt32(Eval("ItemCount")) || Container.ItemIndex ==6? "" : "|"%></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="position: absolute;">
<table class="menutop_tab" id="menutop_childtab" cellpadding="0" cellspacing="0">
<tr style="display:none ;" id="childmenutop_<%# Container.ItemIndex+1 %>">
<td class="menubg_left">
</td>
<td class="menubg_center">
</td>
<td class="menubg_right">
</td>
</tr>
<asp:Repeater ID="repchild" runat="server">
<ItemTemplate>
<tr style="display:none " id="childmenu_<%# ((RepeaterItem)Container.Parent.Parent).ItemIndex+1 %>_<%# Container.ItemIndex+1 %>">
<td class="menubg_color">
</td>
<td class="menucontent_center">
<span οnclick="changeLocation('<%#Eval("NavUrl") %>')"><%#Eval("NavName") %></span>
</td>
<td class="menubg_color">
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr style="display:none " id="childmenubut_<%# Container.ItemIndex+1 %>">
<td class="menubg_bleft">
</td>
<td class="menubg_bcenter">
</td>
<td class="menubg_bright">
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</table>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
InitData();
}
protected void InitData()
{
//按全局权限取,只能moss中用
PortalSiteMapProvider mossSiteMapProvider = PortalSiteMapProvider.GlobalNavSiteMapProvider;
//按权限取全局导航菜单
SiteMapNodeCollection oneLevelNodes = mossSiteMapProvider.CurrentNode.RootNode.ChildNodes;
//如果设置了当前全局导航则取当前导航,否则取顶级全局导航
SiteMapNodeCollection lowNavigationBar = mossSiteMapProvider.CurrentNode.ChildNodes;
if (lowNavigationBar.Count > 0 && !mossSiteMapProvider.CurrentWeb.IsRootWeb)
this.BuildTopNavigationBar(lowNavigationBar, mossSiteMapProvider);
else
//获得全局导航
this.BuildTopNavigationBar(oneLevelNodes, mossSiteMapProvider);
}
/// <summary>
/// 构建顶级全局导航
/// </summary>
/// <param name="topNavigationBar"></param>
/// <param name="mossSiteMapProvider"></param>
/// <param name="rightList"></param>
/// <returns></returns>
private void BuildTopNavigationBar(SiteMapNodeCollection topNavigationBar, PortalSiteMapProvider mossSiteMapProvider)
{
//取得当前页面的url
String currentUrl = System.Web.HttpContext.Current.Request.Url.ToString();
SPWeb rootWeb = mossSiteMapProvider.CurrentSite.RootWeb;
List<NavigationModel> navlist = new List<NavigationModel>();
NavigationModel nav = null;
nav = new NavigationModel();
nav.NavName = rootWeb.Title;
nav.NavUrl = rootWeb.Url;
navlist.Add(nav);
foreach (PortalSiteMapNode spNavNode in topNavigationBar)
{
nav = new NavigationModel();
String thisUrl = spNavNode.Url;
if (!thisUrl.Contains(rootWeb.Url))
thisUrl = rootWeb.Url + thisUrl;
try
{
nav.IsNewWindow = "yes";
nav.NavUrl = thisUrl;
nav.NavName = spNavNode.Title;
nav.ChildList = this.GetChildNavList(spNavNode);
if (nav.ChildList != null && nav.ChildList.Count > 0)
nav.IsHaveChild = true;
else
nav.IsHaveChild = false;
nav.ItemCount = topNavigationBar.Count;
navlist.Add(nav);
//Boolean isInGlobal = true;
//if (isInGlobal)//让当前导航选中(暂时不用注释留着)
//{
//}
}
catch (Exception)
{
this.Response.Write("导航暂时无法显示,请联系管理员!");
}
}
this.repNav.DataSource = navlist;
this.repNav.DataBind();
}
/// <summary>
/// 查询子导航
/// </summary>
/// <param name="parentNode">父级导航</param>
/// <returns></returns>
public List<NavigationModel> GetChildNavList(PortalSiteMapNode parentNode)
{
List<NavigationModel> navlist = new List<NavigationModel>();
NavigationModel nav = null;
if (parentNode.ChildNodes.Count > 0)
{
foreach (SiteMapNode item in parentNode.ChildNodes)
{
nav = new NavigationModel();
nav.NavName = item.Title;
nav.NavUrl = item.Url;
navlist.Add(nav);
}
}
return navlist;
}
protected void repNav_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断当前是否属于项
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem != null)
{
List<NavigationModel> navlist = null;
NavigationModel nav = null;
nav = (NavigationModel)e.Item.DataItem;
if (nav == null)
return;
navlist = nav.ChildList;
if (navlist != null && navlist.Count > 0)
{
Repeater rep = (Repeater)e.Item.FindControl("repChild");
rep.DataSource = navlist;
rep.DataBind();
}
}
}
}